当id具有文件扩展名时,ASP.NET MVC 4路由无法正常工作

时间:2021-06-18 04:03:19

In MVC 3 I didn't have an issue, but now I have gone to MVC 4 I do.

在MVC 3中我没有问题,但现在我已经去了MVC 4。

The URL looked like this:

网址如下所示:

{controller}/{action}/{id}

/podcast/file/PODCAST-01.MP4

Where the PODCAST-01.MP4 is the id; when it hits the controller, the controller would then send the file to the client, it was done this way so that we could easily count how many people downloaded the file.

其中PODCAST-01.MP4是id;当它到达控制器时,控制器然后将文件发送到客户端,它以这种方式完成,这样我们就可以轻松计算下载文件的人数。

However, now, the web server, thinks there should be a file there and gives a 404 error. I have tried to get around this by making my own 404 error page and then redirecting back with /podcast/file?id=PODCAST-01.MP4 but this doesn't work because iTunes doesn't like it, it doesn't like the "?".

但是,现在,Web服务器认为应该有一个文件并给出404错误。我试图通过制作我自己的404错误页面,然后使用/podcast/file?id=PODCAST-01.MP4重定向回来解决这个问题,但这不起作用,因为iTunes不喜欢它,它不喜欢“?”。

I tried creating a a new route:

我尝试创建一条新路线:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}.{format}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, format = UrlParameter.Optional });

But that seems to have just broken the routing. Any help would be great.

但这似乎刚刚破坏了路由。任何帮助都会很棒。

2 个解决方案

#1


7  

You can do that. In MVC 4 solution web config contains:

你可以做到这一点。在MVC 4解决方案中,Web配置包含:

<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />

You simply have to add before those a new handler for you path

您只需在为路径添加新处理程序之前添加

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

#2


0  

You cannot do that (I think) You have to rest the suffix in your code analysing the Request.

你不能这样做(我认为)你必须在代码中休息后缀分析请求。

#1


7  

You can do that. In MVC 4 solution web config contains:

你可以做到这一点。在MVC 4解决方案中,Web配置包含:

<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />

You simply have to add before those a new handler for you path

您只需在为路径添加新处理程序之前添加

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

#2


0  

You cannot do that (I think) You have to rest the suffix in your code analysing the Request.

你不能这样做(我认为)你必须在代码中休息后缀分析请求。