ASP.NET MVC Dynamic Routes and Action Links with Arbitrary Depth [duplicate]

ぐ巨炮叔叔 提交于 2019-11-30 10:15:15
DSO

If you need deep and/or non-conforming URLs, I would suggest that you employ attribute based routing, such as the solution discussed here.

I prefer an attribute based approach over putting every route in Application_Start, because you have better locality of reference, meaning the route specification and the controller which handles it is close together.

Here is how your controller actions would look for your example, using the UrlRoute framework I implemented (available on codeplex):

[UrlRoute(Path = "Forum")]
public ActionResult Index()
{
    ...
}

[UrlRoute(Path = "Forum/General-Discussion")]
public ActionResult GeneralDiscussion()
{
    ...
}

[UrlRoute(Path = "Forum/Technical-Support/Product/{productId}")]
public ActionResult ProductDetails(string productId)
{
    ...
}

[UrlRoute(Path = "Forum/Technical-Support/Website/{topicId}/{pageNum}")]
[UrlRouteParameterDefault(Name = "pageNum", Value = "1")]
public ActionResult SupportTopic(string topicId, int pageNum)
{
    ...
}

With this approach you can generate outbound URLs using the same helpers (Url.Route*, Url.Action*) that you would use if you manually added the routes using the default route handler, no extra work needed there.

You could have them all go to one controller action which handles the route handling for you by manually splitting the rest of the url out and calls what a method on your BLL which then delegates tasks to other methods finally returning a View() depending on your needs.

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