IIS7.5 上的 MVC3 应用程序与文件 x.jpg 的路由不起作用
当我干净的、刚刚创建的新应用程序(集成.net 4.0)位于 Visual Studio Web Server 上时,一切正常。像下面这样的链接工作正常并且控制器返回图像。
http://localhost:12345/image/a.jpg
但是,当我在 IIS 7.5 上运行此应用程序时,iis 会控制并报告 404。
http://localhost/testmvc3/image/a.jpg
控制器:
public class ImageController : Controller
{
public ActionResult Index(string name)
{
var dir = Server.MapPath("~/content/" + name);
return File(dir, "image/jpg");
}
}
路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Image", // Route name
"image/{*name}", // URL with parameters
new { controller = "Image", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
我应该更改什么才能正确运行此应用程序?
编辑1:
问题在于扩展。当我删除扩展名时,请求指向图像控制器。对于扩展名 (jpg),iis 首先接受请求(为什么!?)并返回 404(不接触图像控制器操作)。
编辑2:
- 在我进行更改之前,Windows 7 64位
- 应用程序上的IIS 7.5在Framework 4.0上集成了管道
web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
When my clean, just created new app (.net 4.0 integrated) is on Visual Studio Web Server everything works fine. Link like this below works fine and controller returns image.
http://localhost:12345/image/a.jpg
But when I run this app on IIS 7.5 then iis takes control and reports 404.
http://localhost/testmvc3/image/a.jpg
Controller:
public class ImageController : Controller
{
public ActionResult Index(string name)
{
var dir = Server.MapPath("~/content/" + name);
return File(dir, "image/jpg");
}
}
Routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Image", // Route name
"image/{*name}", // URL with parameters
new { controller = "Image", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
What I should change to run this app properly?
EDIT1:
The problem is with extension. When I remove extension then requests points to image controller. With extension (jpg) iis takes request first (why!?) and returns 404 (without touching image controller action).
EDIT2:
- IIS 7.5 on Windows 7 64bit
- App on Framework 4.0 integrated pipeline
web.config before my changes:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了。呃.....
我将 ExtensionlessUrlHandler 路径从默认的“*.”更改为“*”:
现在所有请求都将通过路由引擎。
然后,我将 IgnoreRoute 添加到“content/{*all}”,其中包含所有静态内容文件。
I found it. Uchh.....
I change ExtensionlessUrlHandler path from default '*.' to '*':
Now all requests are going through routing engine.
Then I add a IgnoreRoute to 'content/{*all}' where I have all static content files.
如果解决方案已部署,即不仅根据您的解决方案进行映射,而是按应有的方式进行部署,则
Server.MapPath
的结果将不会如您所期望的那样。Server.MapPath
“默认”文件夹是实际 dll 所在的位置。解决此问题的一种方法是将图像的“复制到输出目录”设置为“复制始终”或“如果较新则复制”。
您也许可以使用类似
Server.MapPath("~/content" + name);
的东西,但我这会让您的解决方案找不到图像。If the solution is deployed, that is, not only mapped against your solution but instead deployed as it should be, the result of
Server.MapPath
will not be as you expect. TheServer.MapPath
"default" folder is where the actual dll is.One way you can solve this on is to set the "Copy to Output Directory" for your images to "Copy alwats" or "Copy if newer".
You could maybe use something like
Server.MapPath("~/content" + name);
, but I that will make your solution not finding the images.在项目的“属性”中,在“Web”选项卡上,在单选按钮
使用本地 IIS Web 服务器
下方的部分中,在项目 URL 框中,将条目更改为http://本地主机:12345
。然后,在 IIS 中,编辑 localhost 站点的 http 绑定,并将其更改为 12345。
In the Properties for your project, on the Web tab, in the section below the radio button
Use Local IIS Web Server
, in the box for Project Url, change the entry tohttp://localhost:12345
.Then, in IIS, edit the http binding for the localhost site, and change it to 12345.
您不应该被要求这样做。确保您的应用程序池正在集成管道模式下运行。
you should not be required to do that. Make sure your application pool is running in Integrated pipeline mode.