9

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
Paul A.T. Wilson
  • 854
  • 2
  • 9
  • 18
  • 2
    When you say it breaks the routing, what happens? It looks correct at a glance. – Billdr Jan 11 '13 at 12:34
  • I get this error when I go to any page: "HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory." when I change the RouteConfig.cs – Paul A.T. Wilson Jan 11 '13 at 15:15
  • How about adding a `constraints` section that explicitly defines what a file name should have (and not have)? (there's also, historically, been trouble with [consecutive optional parameters](http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx)) – Brad Christie Jan 11 '13 at 15:34
  • 7
    For this route `{controller}/{action}/{id}.{format}`, you need to escape the dot because routes are actually regular expressions. What happens if you use `{controller}/{action}/{id}\.{format}`? – w.brian Jan 11 '13 at 15:34
  • possible duplicate of [Semantic urls with dots in .net](http://stackoverflow.com/questions/294495/semantic-urls-with-dots-in-net) – John Koerner Jan 11 '13 at 16:03
  • Thanks for trying guys, doesn't seem to be anything I can do about it. In the end I just handed it in a customer 404 error page. – Paul A.T. Wilson Jan 12 '13 at 13:04
  • routes.MapRoute("Default","{controller}/{action}/{file}.{format}", new { controller = "Home", action = "Index", file = UrlParameter.Optional, format = UrlParameter.Optional }); It works ok, I guess your problem is an order in which you defined routing. The order of routes is significant. Please show us whole routing configuration. – Maciej Rogoziński Jan 15 '13 at 11:12

2 Answers2

8

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" />
Radu D
  • 3,505
  • 9
  • 42
  • 69
0

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