ASP.NET MVC2 路由 IIS6 - 仅默认路由有效
我在运行 IIS 6 的 Windows Server 2003 计算机上部署了 ASP.NET MVC2 网站。 我在标准 MVC 项目中使用了几乎默认路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Products", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
导航到 http://localhost/MyApplication 需要我到列表页面就好了。导航到 http://localhost/MyApplication/Products/Details/21 给我一个 404。该路由在 VS2010 的内置开发服务器上运行良好。
我已经放入了到处都提到的标准 IIS 通配符 aspnet_isapi.dll 映射 - 导航到“列表”页面在我这样做之前不起作用 - 但导航到默认路由以外的任何内容都被破坏。
我真的很想保留我的无扩展名网址。有谁知道为什么路由适用于默认网页,但不适用于其他网页?
*编辑:刚刚尝试添加 .aspx 扩展名,即现在我的路线如下所示:
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
并且它具有相同的行为,除了这次我得到 asp 404 页面而不是 html 404 页面...
*编辑 2:尝试过再次使用以下路线并确保 .mvc 已映射到 aspnet_isapi.dll:
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
这次我收到“Internet Explorer 无法显示网页”404 样式页面。我现在使用这 3 种不同的方法遇到了 3 个不同的 404 错误...
*编辑 3 编辑返回:我在 Windows XP Professional 上的 IIS 5.1 中运行该站点,仅在虚拟目录上进行了通配符重新映射,但是天堂禁止我可以让它在网络服务器上运行......
I've deployed an ASP.NET MVC2 website on a Windows Server 2003 machine running IIS 6.
I'm using pretty much the default routing in a standard MVC project:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Products", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
Navigating to http://localhost/MyApplication takes me to the List page just fine. Navigating to http://localhost/MyApplication/Products/Details/21 gives me a 404. This routing worked fine on the inbuilt development server in VS2010.
I've put in the standard IIS wildcard aspnet_isapi.dll mapping mentioned all over the place - navigating to the List page didn't work before I did - but navigating to anything other than the default route is broken.
I'd really like to keep my extensionless URLs. Does anyone have any idea as to why the routing would work for the default webpage, but no others?
*Edit: just tried adding the .aspx extension, ie now my route looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
And it has the same behaviour, except this time I get the asp 404 page instead of the html 404 page...
*Edit 2: tried it again using the following route and making sure .mvc was mapped to aspnet_isapi.dll:
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
This time I got an 'Internet Explorer cannot display the webpage' 404-style page. I've now got 3 different 404 errors from using these 3 different methods...
*Edit 3 Return of the Edit: I have the site running in IIS 5.1 on Windows XP professional with only a wildcard remapping on the virtual directory, but heaven forbid I can get it to run on the webserver...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该遵循以下演练: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
(只是为了确保您没有错过某物)。这展示了如何使无扩展和扩展版本正常工作。您可能至少需要检查扩展版本是否有效,以检查其他内容是否配置错误。
之后,也许您应该尝试添加类似的内容..
http: //haacked.com/archive/2008/03/13/url-routing-debugger.aspx
...因为它确实可以帮助确定您的路由在哪里被破坏。
其他一些需要检查的事项:
IIS 是否配置了 ASP.NET?检查虚拟目录的属性。在 VDir 上选择配置按钮并检查常用的 ASP.NET 扩展是否已映射到 .NET 2.0 ISAPI dll。这将类似于
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll。
是否在 IIS 上启用了 ASP.NET 2(在 IIS 6 下,默认情况下处于关闭状态):在 IIS 管理器中,检查 Web 服务扩展文件夹以启用它。
您部署了global.asax吗?
First of all, you should follow this walk-through: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
(just to make sure you've not missed something). This shows how to get the extension-less and extention'd versions working. You might want to at least check whether the extension'd version works to check that other things aren't misconfigured.
After that, perhaps you should try adding something like this..
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
... as it can really help in determining where your routes are broken.
Some other things to check:
Is IIS configured with ASP.NET? Check properties of the virtual directory. On the VDir select the configuration button and check that the usual ASP.NET extensions are mapped to the .NET 2.0 ISAPI dll. This will be something along the lines of
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.
Is ASP.NET 2 enabled on IIS (under IIS 6 this is off by default): in IIS manager check the Web Service Extensions folder to enable it.
Have you deployed global.asax?
找到了 - 路由实际上有效,但我的 URL 无效。我正在使用 javascript 构建一些 URL,结果发现我没有链接到
http://localhost/MyApplication/Controller/Action/ID
我实际上链接到了
http://localhost/Controller/Action/ID
像这样构建的链接在开发服务器上正常工作,但是一旦将站点部署到 Web 服务器上的虚拟目录,这些地址就会因为 URL 中存在额外的应用程序名称而变得不正确。
总之,请小心您的 URL - 不要像我一样用字符串构建它们。
Found it - the routing is actually working, my URLs were not. I was using javascript to build some of the URLs and it turns out that I wasn't linking to
http: //localhost/MyApplication/Controller/Action/ID
I was actually linking to
http: //localhost/Controller/Action/ID
Building the links like this was working on the development server, but once the site was deployed to a virtual directory on the webserver those addresses are incorrect because of the extra application name in the URL.
In conclusion, be careful with your URLs - don't build them out of strings like I did.