【问题标题】:MVC catch-all route is not being called未调用 MVC 包罗万象的路线
【发布时间】:2015-06-29 20:21:24
【问题描述】:

目前我正在使用 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 方法永远不会被调用。对于我可能遗漏或做错的任何提示,我将非常感谢。

谢谢

【问题讨论】:

  • 如果你只是在 url 中输入你的路径会发生什么?它是否显示 .jpg 图像?

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


【解决方案1】:

我现在找到了原因,为什么我的控制器没有被调用。如果请求 url 包含“.”,静态文件处理程序将尝试处理请求。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-20
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多