首页 > 解决方案 > RouteConfig 和带有扩展名的文件名 - MVC5

问题描述

我有以下网址:

/Meetings/doc1.pdf
/Meetings/doc2.pdf

我尝试在我的 RouteConfig 中设置以下内容

var route = routes.MapRoute(
                name: "RedirectMeetingDocs",
                url: "Meetings/{filename}",               
                defaults: new { action= "GetAttachmentByName", controller = "Generic" },
                constraints: new { filename = @"(.*?)\.(pdf|ppt)" }
            );

具有以下功能:

[HttpGet]
        public ActionResult GetAttachmentByName(string fileName)
        {
...
}

但是,它永远无法正常工作,并且我不断收到 404 错误。

我也试过:

var route = routes.MapRoute(
            name: "RedirectMeetingDocs",
            url: "Meetings/{filename}.{extension}",
          
            defaults: new { action= "GetAttachmentByName", controller = "Generic" },
            constraints: new { filename = new AlphaRouteConstraint(), extension = new AlphaRouteConstraint() }
        );

public ActionResult GetAttachmentByName(string fileName, string extension)
        {

但没有任何效果。

有什么建议吗?

标签: asp.net-mvcroutes

解决方案


好的,我通过在我的 Web.config 中添加以下内容来解决它:

<add name="IATTCRedirectOldMeetingFiles" path="meetings/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

现在,上面显示的两个 RouteConfig 都可以工作。


推荐阅读