目前,我正在开发一个与MVC5的网站,在那里我需要从项目文件夹外提供一些图像。img-src的示例url为:
<img src="/Image/sub1/sub2/imgname.jpg" />我现在的想法是,我可以使用一个包罗万象的路线。因此,我创建了以下路由:
routes.MapRoute(
name: "Image",
url: "Image/{*subpath}",
defaults: new { controller = "Test", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);和相应的控制器:
public class TestController : BaseController {
public ActionResult Index(string subpath) {
var path = Path.Combine(ImageFolderPath, subpath);
return new FileStreamResult(
new FileStream(path, FileMode.Open), "image/jpeg"
);
}
}我现在的问题是,控制器或它的一个方法没有被调用。FireBug总是告诉我,图像返回的是404。
我还尝试了MVC5属性路由:
// in RouteConfig.cs
routes.MapMvcAttributeRoutes();
// controller
[Route("image/{*subpath}")]
public class TestController : BaseController {
public ActionResult Index(string subpath) {
var path = Path.Combine(ImageFolderPath, subpath);
return new FileStreamResult(
new FileStream(path, FileMode.Open), "image/jpeg"
);
}
}但结果仍然是一样的。我的index方法永远不会被调用。我将非常感谢任何关于我可能遗漏或做错了什么的提示。
谢谢
发布于 2015-06-29 20:21:25
我现在找到了原因,为什么我的控制器没有被调用。如果请求url包含'.',静态文件处理程序将尝试处理该请求。
可以在以下位置找到解决方法:SO
https://stackoverflow.com/questions/31115080
复制相似问题