ASP.NET MVC从URL获取路线值 [英] ASP.NET MVC Get Route values from a URL
问题描述
我想工作的控制器动作为 UrlReferrer
的路由值是什么。
I want to work out what the route values for the UrlReferrer
in the controller action would be.
我不能哪些部分在MVC管道输入的URL转换为RouteValues搞清楚,我想要达到的接近。
I can't figure out at what part in the MVC pipeline the incoming URL is converted into RouteValues, what I'm trying to achieve is close to that.
推荐答案
您需要调用 RouteTable.Routes.GetRouteData code>带嘲讽
HttpContextBase
返回您的网址在其请求
。
You need call RouteTable.Routes.GetRouteData
with a mocked HttpContextBase
which returns your URL in its Request
.
的路由使用请求的鸭prelativeCurrentExecutionFilePath
内部匹配。结果
但是,此功能不外露,所以你需要传递一个 HttpContextBase
。
The routes are matched internally using the request's AppRelativeCurrentExecutionFilePath
.
However, this functionality is not exposed, so you need to pass an HttpContextBase
.
您需要创建一个在其请求属性返回一个的Htt prequestBase
实例的 HttpContextBase
类。结果
在的Htt prequestBase
类需要返回路径,以〜/
开始,在其鸭prelativeCurrentExecutionFilePath
属性。
You need to create an HttpContextBase
class which returns an HttpRequestBase
instance in its request property.
The HttpRequestBase
class needs to return your path, beginning with ~/
, in its AppRelativeCurrentExecutionFilePath
property.
您不需要执行任何其他属性,除非他们使用 IRouteConstraint
秒。
You don't need to implement any other properties, unless they're used by IRouteConstraint
s.
有人已经写了这个:<一href=\"http://web.archive.org/web/20150223104725/http://www.scottschluer.com/creating-a-routedata-instance-from-a-url/\"相对=nofollow>从网址创建的RouteData实例
Someone already wrote this: Creating a RouteData instance from a URL
这篇关于ASP.NET MVC从URL获取路线值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!