任意深度的 ASP.NET MVC 动态路由和操作链接

发布于 2024-07-25 20:23:02 字数 1199 浏览 8 评论 0原文

我想用 ASP.NET MVC 组合一个论坛/留言板。 在这些类型的论坛上很常见的是分层板类别,例如:

-一般讨论
-技术支持
--网站技术支持
--产品技术支持
---产品A技术支持
---产品 B 技术支持

每个类别下面是主题和属于这些主题的消息。 我主要关心的是 1.) 在给定 URL 的情况下到达正确的位置,2.) 不在我的 URL 中包含大量不必要的信息,以及 3.) 能够从代码重新创建 URL。

我希望 URL 是这样的:

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

现在,我已从中排除了操作名称,因为可以根据我所在的位置推断出它们。 我的数据库中的每个 Board 实体都有一个“UrlPart”列,该列已编入索引,因此我希望能够对该表进行相对快速的查询以找出我所在的位置。

问题是:为了找出正确的位置,我应该使用自定义路由处理程序、自定义路由绑定器,还是应该创建模糊的路由规则?

这个建议看起来不错,但看起来也很费力却收效甚微: 用于搜索的 ASP.NET MVC 自定义路由

这似乎表明创建模型绑定会更容易: MVC 动态路由

为了实现#3,我必须创建自己的自定义 URL 生成逻辑,正确的?

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:

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

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.

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

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?

This suggestion looks pretty good but it also looks like a lot of work for little benefit:
ASP.NET MVC custom routing for search

This seems to indicate that creating a model binding would be easier:
MVC Dynamic Routes

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-08-01 20:23:02

如果您需要深层和/或不合格的 URL,我建议您使用基于属性的路由,例如讨论的解决方案 此处

与将每个路由放入 Application_Start 相比,我更喜欢基于属性的方法,因为您拥有更好的引用局部性,这意味着路由规范和处理它的控制器紧密结合在一起。

以下是您的示例中控制器操作的查找方式,使用我实现的 UrlRoute 框架(可在 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*)生成出站 URL,无需额外工作。

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.

水水月牙 2024-08-01 20:23:02

您可以让它们全部转到一个控制器操作,该操作通过手动拆分 url 的其余部分并调用 BLL 上的方法来为您处理路由处理,然后将任务委托给其他方法,最终根据您的情况返回 View()需要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文