.mvc和无扩展名URL的MVC路由(MVC routing for both .mvc and extensionless URLs)

编程入门 行业动态 更新时间:2024-10-27 09:35:57
.mvc和无扩展名URL的MVC路由(MVC routing for both .mvc and extensionless URLs)

我有一个奇怪的问题。 我正在开发一些设置为无扩展的MVC代码,如/Home/Index?id=10但是,它曾经是为IIS 6设置的,并且在所有控制器上使用.mvc扩展名,如/Home.mvc/Index?id=10 。

我希望这两条路线都能够工作。 在我的global.asax.cs文件中,我创建了以下代码:

public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute("Default2", "{controller}.mvc/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }

几乎奏效了! 但不幸的是,当我的控制器返回RedirectToAction时,生成的URL失败。

我这样调用RedirectToAction:

return RedirectToAction("Index", "Account");

它返回:

/Account.mvc

现在假设索引,所以我不担心这个缺失。 但是,该URL的末尾没有/ ,因此,它返回404错误。 如果我添加/它工作正常。 有什么方法可以帮助MVC正确生成URL,还是有一种方法可以修改路由以使其识别此URL?

背景:我遇到这种情况是因为在我们将Web服务器升级到IIS 7.5之后,我开始将我的应用程序转换为使用无扩展名的URL。 我没有意识到与外部世界共享的应用程序有很多链接 - 改为无扩展程序打破了所有这些链接。 卫生署! 应该留下足够好的一个人。

I have an odd question. I'm working on some MVC code that was setup to run extensionless, as in /Home/Index?id=10 However, it used to be setup for IIS 6, and was using the .mvc extension on all the controllers, as in /Home.mvc/Index?id=10.

I would like both routes to be able to work. In my global.asax.cs file, I created the following code:

public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute("Default2", "{controller}.mvc/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }

This almost worked! But unfortunately when my controllers return RedirectToAction, the URL which is generated fails.

I call RedirectToAction like this:

return RedirectToAction("Index", "Account");

and it returns this:

/Account.mvc

Now Index is assumed so I'm not worried that that's missing. However, there is no / on the end of that URL, and because of that, it is returning a 404 error. If I add the / it works fine. Is there some way I can help MVC to generate the URLs correctly, or is there a way I can modify the routes to make it recognize this URL?

Background: I'm in this situation because I began converting my application to use extensionless URLs after we upgraded our webserver to IIS 7.5. I didn't realize that there were alot of links into the applications which had been shared with the outside world--changing to extensionless broke all those links. Doh! Should've left well enough alone.

最满意答案

我个人会使用URL重写器 。 您可以使用IIS重写模块将ASP.NET旧版URL重定向到Extensionless 。

例如,您可以创建一个类似的出站规则(不完全是,它们肯定需要测试):

<outboundRules> <rule name="remove .mvc outbound 1"> <match serverVariable="RESPONSE_LOCATION" pattern="^/(.*).mvc$" /> <action type="Rewrite" value="/{R:1}" /> </rule> <rule name="remove .mvc outbound 2"> <match filterByTags="A, Form, IFrame, Img, Input, Link, Script" pattern="^/(.*).mvc$" /> <action type="Rewrite" value="/{R:1}" /> </rule> </outboundRules>

如果有人决定对MVC扩展进行硬编码......

<rule name="Add Slash Inbound"> <match url="(.*)" /> <conditions> <add input="{PATH_INFO}" pattern="^/(.*).mvc$" negate="true" /> </conditions> <action type="Rewrite" url="{R:0}/" /> </rule>

For anyone trying to figure this out: the problem may not be that RedirectToAction is generating a bad URL, but actually that the URL is not being interpreted properly by IIS.

I discovered that other servers had no problem with the missing slash in the URLs described in the original question.

This led me to look at the App Pool setting. One setting in particular seems to affect this: "Enabled 32-bit applications". Setting this to true on the Application Pool enabled my application to parse URLs which were missing the trailing '/'.

Weird huh?

更多推荐

URL,Index,id,MVC,电脑培训,计算机培训,IT培训"/> <meta name="description&

本文发布于:2023-08-07 23:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:扩展名   路由   URL   mvc   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!