从.net API中路由获取参数

时间:2018-11-10 11:30:31

标签: asp.net .net asp.net-apicontroller

我的Web api项目具有以下路线:

 config.Routes.MapHttpRoute(
            name: "Default_Api",
            routeTemplate: "api/v{version:apiVersion}/{lang}/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

然后我需要将{lang}参数放入Application_BeginRequest

 var httpContext = HttpContext.Current;
        RouteData currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
        string lang = currentRouteData.Values["lang"] as string;

,但始终返回null 我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用HttpContext.Current.Request.RequestContext.RouteData