MVC 路由:尝试使动态根值发挥作用

发布于 2024-11-16 05:45:35 字数 1002 浏览 3 评论 0 原文

我正在尝试使用网站的根 url 定义网站的动态部分。我在为其定义正确的 MVC 路线时遇到一些麻烦。有人可以帮忙吗?

我想要的网址将如下所示: http://website.com/[dynamic-string]

但我还有其他标准页面,例如: http://website.com/abouthttp://website.com/常见问题解答 甚至只是http://website.com

我的路线无法使用该动态字符串正常工作。如下图所示。

这是动态字符串的路由。

    routes.MapRoute(
    "CommunityName", // Route name
    "{communityName}", // URL with parameters
    new { controller = "Community", action = "Community", communityName = UrlParameter.Optional }
  );

这是所有其他标准页的路线。

 routes.MapRoute(
 "Default", // Route name
 "{action}", // URL with parameters
 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
 );

我的路线就是不匹配。一切要么被转移到一条路线,要么转移到另一条路线,具体取决于首先声明的路线。

I am trying to define dynamic sections of my site with the root url of the site. I am having some trouble defining the right MVC Route for it. Can someone please help.

My desired url will look like this: http://website.com/[dynamic-string]

But I have other standard pages like: http://website.com/about or http://website.com/faq or even just http://website.com.

My routes don't work correctly with that dynamic string. As shown below.

This is the route for the dynamic-string.

    routes.MapRoute(
    "CommunityName", // Route name
    "{communityName}", // URL with parameters
    new { controller = "Community", action = "Community", communityName = UrlParameter.Optional }
  );

This is the route for all other STANDARD PAGES

 routes.MapRoute(
 "Default", // Route name
 "{action}", // URL with parameters
 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
 );

My routes just don't match up. Everything either gets diverted to one or the other route depending on which route is declared first.

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

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

发布评论

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

评论(2

遥远的她 2024-11-23 05:45:35

你提到的两条路线没有区别。 MVC 如何知道哪个 url 应该映射到communityName 以及哪个url 应该映射到action?任何 url 都可以匹配。

您可以将标准页面定义为路由(在 CommunityName 路由之前),也可以在 Community 操作中捕获它们,查看名称是否与 Home 控制器中的函数匹配,然后调用正确的操作函数。

我以前从未这样做过,但您也许可以创建一个更智能的路由处理程序来查看控制器操作,检查该操作是否确实存在,如果为真则选择该路由。

There is no difference between the two routes you mention. How can MVC know which url should be mapped to communityName and which to action? Any url can match both.

You can define your standard pages as a route (before the CommunityName route) or you can catch them in your Community action, see if the name matches a function in your Home controller and then call the right action function.

I've never done this before but you might be able to create a more intelligent routehandler that looks at your controller actions, checks if the action really exists and if true selects that route.

美人如玉 2024-11-23 05:45:35

这是因为路线实际上是相同的。当您声明操作路线时,您不会声明对该路线的任何约束,因此任何内容都将被假定为操作名称。

如果您希望两个路由在同一级别捕获,那么您必须将操作名称限制为控制器上存在的操作名称,这样,如果不匹配,它将传递到下一个路由。

您可以在此处查看高级约束的示例:

http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Custom-route-constraint-to-validate-against-a-list.aspx

this is beacuse the routes are effectively the same. When you declare the action route you do not state any constraints to the route, for this reason anything will be assumed to be a the action name.

If you want two routes to capture at the same level then you must constrain the action names to those that exist on your controller, this way if it does not match it will pass to the next route.

You can see an example of and advanced constraint here:

http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Custom-route-constraint-to-validate-against-a-list.aspx

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