Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

asp.net mvc - Routing a url with extension in MVC4 won't work, tries to serve up static file

I'm using MVC4 and need to route a request like this to a controller:

[myapp]/data/fileinfo.xml

Here is the route I have configured:

routes.MapRoute(
            name: "Data",
            url: "Data/{file}",
            defaults: new { controller = "Data", action = "fileinfo"}
        );

Now, this works perfectly fine and routes requests to my DataController if the URL does not include the .xml extension, but as soon as an extension is used, IIS tries to serve up a static file (instead of routing to my controller) and I get a 404 error.

I've read loads of questions/answers about this issue online, and every solution I've tried has failed.

For example, I've tried using RouteExistingFiles = true when configuring my RouteCollection, and I've added <modules runAllManagedModulesForAllRequests="true" /> in web.config, but to no avail.

If anyone has an idea of what I should try or what I may be missing, it would be much appreciated. I'm using asp.Net 4.5, VS 2012 and IIS 8.0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can add this to your web.config in the <system.webServer><handlers> section:

<add name="ManagedDllExtension" 
     path="data/fileinfo.xml" 
     verb="GET" type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

Your route would be

routes.MapRoute(
        name: "Data",
        url: "Data/fileinfo.xml",
        defaults: new { controller = "Data", action = "fileinfo"}
    );

There is also <modules runAllManagedModulesForAllRequests="true"> but it doesn't seem to work for MVC4/IIS8 (used to be ok in MVC3/IIS7 IIRC). More info here. There is also a performance impact with this one as every request will route through the managed pipeline.

HTH


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...