web api get route template from inside handler

后端 未结 4 1533
春和景丽
春和景丽 2021-01-12 06:05

I searched a lot before putting the questions here but the more I search the more confused I get.

So I have created an handler and I am trying to get the route like

4条回答
  •  佛祖请我去吃肉
    2021-01-12 06:25

    I think you can get route Data from request.Properties property and easy to unit test.

    /// 
        /// Gets the  for the given request or null if not available.
        /// 
        /// The HTTP request.
        /// The  or null.
        public static IHttpRouteData GetRouteData(this HttpRequestMessage request)
        {
            if (request == null)
            {`enter code here`
                throw Error.ArgumentNull("request");
            }
    
            return request.GetProperty(HttpPropertyKeys.HttpRouteDataKey);
        }
    
        private static T GetProperty(this HttpRequestMessage request, string key)
        {
            T value;
            request.Properties.TryGetValue(key, out value);
            return value;
        }
    

    Reference link of code

提交回复
热议问题