问题描述
我想知道是否有可能以某种方式在控制器之外获取路由值?
这是我尝试过的.我也尝试做.Values["{projectId}"];
,但我仍然得到null
.
[RoutePrefix("projects")]
public class UsergroupController : ApiController
{
[Route("{projectId}/usergroups"), HttpGet]
public IHttpActionResult Get()
{
// Url: /projects/123/usergroups
// Response: null
var projectId = HttpContext.Current.Request.RequestContext.RouteData.Values["projectId"];
return Ok(projectId);
}
}
我发现它位于,但是非常奇怪.也许可能会有某种LINQ查询来获取它?
RouteData-Values-Values-0-0-Values-0
WebApi具有它自己的嵌套RouteData,这是因为它不使用System.Web
中的RouteData
类,而是它自己的HttpRouteData
类./p>
WebApi路由在键MS_SubRoutes
下添加一个条目,WebApi将其用于内部路由.
我写了一个扩展方法,将WebApi路由数据展平为string/object
字典,这在RouteData.Values
WebApiExtensions.cs
public static class WebApiExtensions
{
public static IDictionary<string, object> GetWebApiRouteData(this RouteData routeData)
{
if (!routeData.Values.ContainsKey("MS_SubRoutes"))
return new Dictionary<string,object>();
return ((IHttpRouteData[])routeData.Values["MS_SubRoutes"]).SelectMany(x => x.Values).ToDictionary(x => x.Key, y => y.Value);
}
}
用法:
[RoutePrefix("projects")]
public class UsergroupController : ApiController
{
[Route("{projectId}/usergroups"), HttpGet]
public IHttpActionResult Get()
{
// Url: /projects/123/usergroups
// Response: null
object value = null;
var webApiRouteData = HttpContext.Current.Request.RequestContext.RouteData.GetWebApiRouteData();
value = webApiRouteData["projectId"];
return Ok(value);
}
}
I wonder if it's possible to somehow get the route value outside of controller?
This is what I've tried. I also tried to do .Values["{projectId}"];
but I still get null
.
[RoutePrefix("projects")]
public class UsergroupController : ApiController
{
[Route("{projectId}/usergroups"), HttpGet]
public IHttpActionResult Get()
{
// Url: /projects/123/usergroups
// Response: null
var projectId = HttpContext.Current.Request.RequestContext.RouteData.Values["projectId"];
return Ok(projectId);
}
}
I found that it is located, but in a very strange matter. Maybe there might be some kind of LINQ query to get it?
RouteData-Values-Values-0-0-Values-0
WebApi has it's own nested RouteData, this is because it doesn't use the RouteData
class in System.Web
but it's own HttpRouteData
class.
The WebApi routing adds an entry under the key MS_SubRoutes
, which WebApi uses for it's internal routes.
I've written an extension method which flattens the WebApi route data into a string/object
dictionary, which is what you might see in RouteData.Values
WebApiExtensions.cs
public static class WebApiExtensions
{
public static IDictionary<string, object> GetWebApiRouteData(this RouteData routeData)
{
if (!routeData.Values.ContainsKey("MS_SubRoutes"))
return new Dictionary<string,object>();
return ((IHttpRouteData[])routeData.Values["MS_SubRoutes"]).SelectMany(x => x.Values).ToDictionary(x => x.Key, y => y.Value);
}
}
Usage:
[RoutePrefix("projects")]
public class UsergroupController : ApiController
{
[Route("{projectId}/usergroups"), HttpGet]
public IHttpActionResult Get()
{
// Url: /projects/123/usergroups
// Response: null
object value = null;
var webApiRouteData = HttpContext.Current.Request.RequestContext.RouteData.GetWebApiRouteData();
value = webApiRouteData["projectId"];
return Ok(value);
}
}
这篇关于是否可以在控制器外部的运行时获取路由数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!