没有调用MVC catch-all路由

时间:2015-06-29 11:57:06

标签: asp.net-mvc asp.net-mvc-routing

目前我正在使用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"
        );
    }
}

但结果仍然相同。我的索引方法永远不会被调用。我会非常感谢任何关于我可能遗失或做错的提示。

由于

1 个答案:

答案 0 :(得分:1)

我现在找到了原因,为什么我的控制器没有被调用。如果请求网址包含&#39;。&#39;,静态文件处理程序将尝试处理请求。

可在此处找到解决方法:SO

相关问题