开发者

Customizing the url-from-parameters lookup in asp.net mvc

开发者 https://www.devze.com 2022-12-19 16:23 出处:网络
I have a route added by the code routes.MapRoute(\"MyRoute\", \"TheUrl\", new { controller = \"MyController\", action = \"MyAction\" });

I have a route added by the code

routes.MapRoute("MyRoute", "TheUrl", new { controller = "MyController", action = "MyAction" });

I can then do a reverse lookup with the arguments like UrlHelper.Action("MyAction", "MyController"), and it will return a nice url like ~/TheUrl开发者_如何学Go

However, for this route I want the generated URL to be ~/TheUrl?p=2354, with the parameter being some versioning parameter. Is there a way of doing this by mapping the route with some customized route handler or something? The versioning parameter will be non-standard and require some custom code to execute every time the Url is looked up.


I think a UrlHelper extension method would be most ideal and simple here specially.

public string MyRoute(this UrlHelper url)
{
     string versionNumber = GetVersionNumber();  // or w/e is required to get it
     return Url.Action("MyAction", "MyController") + "?p=" + versionNumber;
}   

This would make calling that route much easier in html

<%= Url.MyRoute() %>
0

精彩评论

暂无评论...
验证码 换一张
取 消