ASP.NET MVC 路由问题

发布于 2024-10-15 22:57:46 字数 1104 浏览 3 评论 0 原文

我因路由问题而度过了一段糟糕的时光。希望你能帮助我。

以下是有效的 URL:

http://www.example.com/Stories/Action-Name/StoryName

http://www.example.com/Stories/Action-Name/ (它会自动重定向到 ~/Stories/Action-Name/StoryName,这就是我想要的。

但是,如果我删除尾随的“/”(即 http:// www.example.com/Stories/Action-Name)然后它重定向到:http://www.example.com/StoryName

我不知道这是如何或为什么发生的。

这是我在 Global.asax 中设置的路线:

routes.MapRoute("SurvivorStoriesRedirect", "Stories/Action-Name/{id}", new {
                controller = "Stories",
                action = "Action-Name",
                id = UrlParameter.Optional
            });

请让我知道我做错了什么,

谢谢

-

不知道我是怎么搞砸的,但我刚刚添加了这个:

routes.MapRoute("SurvivorStoriesRedirect", "Stories/{action}/{id}", new {
                controller = "Stories",
                action = "Index",
                id = UrlParameter.Optional
            });

并删除了任何其他路线 。 “故事”控制器,一切似乎都有效。

I'm having a terrible time with a routing issue. Hopefully you can help me out.

Here are the URL's that work:

http://www.example.com/Stories/Action-Name/StoryName

http://www.example.com/Stories/Action-Name/ (it automatically redirects to ~/Stories/Action-Name/StoryName which is what I want.

However, if I remove the trailing "/" (i.e http://www.example.com/Stories/Action-Name) then it redirects to: http://www.example.com/StoryName

I have no idea how or why this is happening.

Here's the route I have setup in Global.asax:

routes.MapRoute("SurvivorStoriesRedirect", "Stories/Action-Name/{id}", new {
                controller = "Stories",
                action = "Action-Name",
                id = UrlParameter.Optional
            });

Please let me know what I'm doing wrong.

Thanks.

EDIT --

Not sure what or how I was messing this up but I just added this:

routes.MapRoute("SurvivorStoriesRedirect", "Stories/{action}/{id}", new {
                controller = "Stories",
                action = "Index",
                id = UrlParameter.Optional
            });

And removed any other routes for the "Stories" controller, and all seemed to work.

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

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

发布评论

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

评论(2

带刺的爱情 2024-10-22 22:57:46

您可能需要创建一个路由来处理默认操作调用。试试这个:

routes.MapRoute("SurvivorStoriesDefault", "Stories/Action-Name", new {
                controller = "Stories",
                action = "Action-Name"
            });

You might need to create a route to handle default action calls. Try this:

routes.MapRoute("SurvivorStoriesDefault", "Stories/Action-Name", new {
                controller = "Stories",
                action = "Action-Name"
            });
浮生面具三千个 2024-10-22 22:57:46

routedebugger ://haacked.com/archive/2010/10/06/introducing-nupack-package-manager.aspx" rel="nofollow">NuGet 或从 Phil 的 post 并且您将能够查看不会显示 < 的匹配项em>为什么,但您会看到如何并很快弄清楚。

Grab Phil Haack's routedebugger from NuGet or download the zip from Phil's post and you'll be able to view the matches which won't show you the why but you'll see the how and soon figure it out.

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