ASP.NET MVC动态路由和行动链接任意深度 [英] ASP.NET MVC Dynamic Routes and Action Links with Arbitrary Depth

查看:126
本文介绍了ASP.NET MVC动态路由和行动链接任意深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想放在一起的论坛/留言板ASP.NET MVC。这些类型的论坛,共同pretty是分层板的类别,因此,例如:

I'd like to put together a forum/message board with ASP.NET MVC. Pretty common on these types of forums are hierarchical board categories, so for instance:

- 通用讨论

- 技术支持

--Website技术支持

- 产品技术支持

---产品技术支持

---产品B技术支持

-General Discussion
-Technical Support
--Website Technical support
--Product Technical Support
---Product A Technical Support
---Product B Technical Support

下面的每个分类均则主题和消息属于那些主题。什么我主要关心的是1)获取到正确的地方,给定一个URL,2)不包括在我的网址不必要的信息容载量,和3)能够重​​新从code的URL。

Below each category are then topics and messages belong to those topics. What I'm primarily concerned with is 1.) getting to the correct place, given a URL, 2.) not including boatloads of unnecessary information in my URL, and 3.) being able to recreate a URL from code.

我想一个URL是这样的:

I'd like a URL to be something like this:


mysite.com/Forum/ - forum index
mysite.com/Forum/General-Discussion/ - board index of "general discussion" 
mysite.com/Forum/Technical-Support/Product/Product-A/ - board index of "Product A Tech Support"
mysite.com/Forum/Technical-Support/Website/Topic1004/ - Topic index of topic with ID 1004 in the "Website Technical Support" board
mysite.com/Forum/Technical-Support/Website/Topic1004/3 - Page 3 of Topic with ID 1004

现在,我已经从这个基础上,我在排除动作的名字,因为他们可以推断。在我的数据库中的每个局的实体有一个UrlPart一栏,这是索引的,所以我希望能够做的比较快的查询,对这个表要弄清楚我在哪里。

Now, I've excluded Action names from this because they can be inferred based on where I am. Each Board entity in my database has a "UrlPart" column, which is indexed, so I expect to be able to do relatively fast queries against that table to figure out where I am.

现在的问题是:为了找出正确的地方,我应该使用自定义的路由处理程序,自定义路线粘结剂,或者我应该只是创造不起眼的路由规则

The question is: in order to figure out the correct place, should I use a custom route handler, a custom route binder, or should I just create obscure routing rules?

这看起来建议pretty好,但它也像很多关于一点好处工作:
<一href=\"http://stackoverflow.com/questions/379558/mvcnet-routing#379823\">http://stackoverflow.com/questions/379558/mvcnet-routing#379823

This suggestion looks pretty good but it also looks like a lot of work for little benefit: http://stackoverflow.com/questions/379558/mvcnet-routing#379823

这似乎表明,建立一个模型绑定会更容易些:
<一href=\"http://stackoverflow.com/questions/296284/mvc-dynamic-routes\">http://stackoverflow.com/questions/296284/mvc-dynamic-routes

This seems to indicate that creating a model binding would be easier: http://stackoverflow.com/questions/296284/mvc-dynamic-routes

要履行#3,我将不得不创建自己的自定义URL生成逻辑,对吧?

To fulfill #3 I'm going to have to create my own custom URL generation logic, right?

推荐答案

如果你需要深和/或不合格的网址,我建议你使用基于属性的路由,如讨论的解决方案,<一个href=\"http://stackoverflow.com/questions/894779/asp-net-mvc-routing-via-method-attributes\">here.

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

我preFER属性为基础的方法上投入的Application_Start每一个路线,因为你有更好的引用的局部性的,这意味着路径规格,并处理它是并拢的控制器。

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.

下面是你的控制器动作会怎样看你的例子,使用UrlRoute框架我实现了(可在 codePLEX ):

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)
{
    ...
}

通过这种方法,你就可以使用相同的佣工(Url.Route *,* Url.Action),如果您手动添加使用默认的路由处理,不需要作额外的工作路线,你会使用出站网址。

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.

这篇关于ASP.NET MVC动态路由和行动链接任意深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆