开发者

MVC3 how to create URLs

I see people using Html.ActionLink() and Url.RouteUrl() etc. etc. But surely this will lead to a maintenance nightmare if routes need to be redesigned?

How are people organising t开发者_JS百科he generation of URLs in a typesafe and manageable way?


Strongly typed URL generation via lambda expressions was available for a period of time during the MVC 1.0 beta timeframe. It was removed since the MVC architecture does not actually have a 1-to-1 mapping between action names and controller method names. See this Phil Haack blog post for details.

It is of course still possible to do it, and assuming you're not using action names that differ from method names, it should work fine.


You can use T4MVC to generate typesafe checks at compile time for your MVC urls.

T4MVC analyses your Controller classes, and generates code that will generate typesafe url's.

Instead of

@Html.ActionLink("New customer", "Create", new { Controller = "Customer", orgID = orgID })

You can use code like:

@Html.ActionLink("New customer", MVC.Customer.Create(orgID))


If you want to call a action you use the Html.ActionLink(). This will create a <a href="..." ></a> hyperlink to chosen action.

If you want to create a url and use it not for a hyperlink, you can use the Url.Content() or the Url.RouteUrl(). The content accepts a string and gerenates a safe url. The Route url takes a route object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜