Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 routing not working when the id has a file extension

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

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.

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 "?".

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.

like image 797
Paul A.T. Wilson Avatar asked Jan 11 '13 12:01

Paul A.T. Wilson


1 Answers

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

<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" />
like image 99
Radu D Avatar answered Nov 15 '22 12:11

Radu D