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.

7
  • 2
    When you say it breaks the routing, what happens? It looks correct at a glance. Commented Jan 11, 2013 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 Commented Jan 11, 2013 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) Commented Jan 11, 2013 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}? Commented Jan 11, 2013 at 15:34
  • possible duplicate of Semantic urls with dots in .net Commented Jan 11, 2013 at 16:03

2 Answers 2

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" />
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.