当我干净,刚刚创建的新应用程序(.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
);
}
我应该更改以正确运行此应用程序?
EDIT1:
问题在于扩展。当我删除扩展时,请求指向图像控制器。随着扩展(jpg)iis首先接受请求(为什么!?)并返回404(不接触图像控制器动作)。
EDIT2:
web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
答案 0 :(得分:1)
我找到了。 Uchh .....
我将ExtensionlessUrlHandler路径从默认的“ *。”更改为“ * ”:
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add
name="ExtensionlessUrlHandler-Integrated-4.0"
path="*"
verb="GET,HEAD,POST,DEBUG"
type="System.Web.Handlers.TransferRequestHandler"
resourceType="Unspecified"
requireAccess="Script"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
现在所有请求都通过路由引擎。
然后我将IgnoreRoute添加到' content / {* all} ',其中包含所有静态内容文件。
答案 1 :(得分:0)
如果部署了解决方案,即不仅映射到您的解决方案,而是按照应有的方式进行部署,Server.MapPath
的结果将不会如您所期望的那样。 Server.MapPath
“默认”文件夹是实际dll所在的位置。
解决此问题的一种方法是将图像的“复制到输出目录”设置为“复制alwats”或“Copy if newer”。
你可能会使用像Server.MapPath("~/content" + name);
这样的东西,但我会让你的解决方案找不到图像。
答案 2 :(得分:0)
在项目的属性中,在“Web”选项卡上,单选按钮Use Local IIS Web Server
下方的部分中,在“项目URL”框中,将条目更改为http://localhost:12345
。
然后,在IIS中,编辑localhost站点的http绑定,并将其更改为12345。
答案 3 :(得分:0)
你不应该被要求这样做。确保您的应用程序池在集成管道模式下运行。