ASP.NET MVC Get Route values from a URL

橙三吉。 提交于 2019-12-01 17:24:15

问题


I want to work out what the route values for the UrlReferrer in the controller action would be.

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.


回答1:


You need call RouteTable.Routes.GetRouteData with a mocked HttpContextBase which returns your URL in its Request.

The routes are matched internally using the request's AppRelativeCurrentExecutionFilePath.
However, this functionality is not exposed, so you need to pass an HttpContextBase.

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.

You don't need to implement any other properties, unless they're used by IRouteConstraints.

Someone already wrote this: Creating a RouteData instance from a URL



来源:https://stackoverflow.com/questions/4993990/asp-net-mvc-get-route-values-from-a-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!