15

Is there any way to statically get route values from a service method (outside of a controller) that is running in a Web API context? For example, I can do the following in ASP.NET MVC:

var mvcHandler = HttpContext.Current.Handler as MvcHandler;
var routeValues = mvcHandler.RequestContext.RouteData.Values;

I'd like to find the equivalent version of this code for Web API.

When I try to debug a sample Web API request and look at HttpContext.Current.Handler it is of type HttpControllerHandler, but this type doesn't have any properties to access route data.

EDIT

To try to help provide some more information. The code I am trying to read the value from is inside of a factory class I have that builds a custom object for my application.

4

2 に答える 2

20

HttpRequestMessage で GetRouteData() 拡張機能を使用できます。これを取得するには、System.Net.Http 名前空間を含める必要があります。

System.Web.Http.Routing.IHttpRouteData routeData = Request.GetRouteData();
于 2013-05-13T17:55:09.327 に答える