ASP MVC 路由

发布于 2024-08-30 19:19:27 字数 230 浏览 4 评论 0原文

现在这可能是一个愚蠢的问题,但我是 mvc 新手,似乎无法让它工作。

这是我希望能够对网址/路线执行的操作:

1)www.domain.com/name/home/index
2) www.domain.com/home/index

其中两个主控制器都是单独的控制器,名称部分将非常但全部必须转到同一个控制器,并且名称应该是其中所有操作的参数。

这有可能吗?感谢您的帮助。

now this is probably an stupid question but i'm new to mvc and can't seem to get it working.

Here is what i would like to be able to do with the urls/routes:

1) www.domain.com/name/home/index
2) www.domain.com/home/index

where both the home controllers are seperate controllers and the name part will very but all must go to the same controller and the name should be an param for all the actions in there.

Is this at all possible? Thanks for your help.

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

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

发布评论

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

评论(1

香橙ぽ 2024-09-06 19:19:27

这可能不是您正在寻找的答案,但我认为更常见的是看到

www.domain.com/home/index
www.domain.com/home/index/name

我最初的想法是重载的 Index 操作方法是有意义的,但是 丹尼尔指出这是不允许的(至少不是以我建议的方式)。

更新的答案...

您的 Index 操作方法可以采用 string name 参数,并且您的路由需要包含类似

routes.MapRoute(
    "Default",
    "{controller}/{action}/{name}",
    new { controller = "Home", action = "Index", name = "" }

在您的操作方法中的快速 null< /code> 检查会告诉您 URL 中是否包含 name

This might not be the answer you're looking for, but I think that it would be more usual to see

www.domain.com/home/index
www.domain.com/home/index/name

My initial thinking was that an overloaded Index action method would make sense, but Daniel pointed out that this not allowed (at least not in the manner I suggested).

Updated answer...

Your Index action method could take a string name argument, and your routes would need to contain something like

routes.MapRoute(
    "Default",
    "{controller}/{action}/{name}",
    new { controller = "Home", action = "Index", name = "" }

In your action method a quick null check will tell you whether a name was included in the URL or not.

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