Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ASP.net MVC4 WebApi route with file-name in it

I'm trying to get the following (and similar) urls to work in my ASP.net MVC4/WebApi project:

http://127.0.0.1:81/api/nav/SpotiFire/SpotiFire.dll

The route responsible for this url looks like this:

        config.Routes.MapHttpRoute(
            name: "Nav",
            routeTemplate: "api/nav/{project}/{assembly}/{namespace}/{type}/{member}",
            defaults: new { controller = "Nav", assembly = RouteParameter.Optional, @namespace = RouteParameter.Optional, type = RouteParameter.Optional, member = RouteParameter.Optional }
        );

It works just fine if I remove the . in the file-name, or if I add a slash behind the URL, but that also means I can't use the Url.Route-methods etc. The error I get is a generic 404-error (image below).

enter image description here

I've tried adding <httpRuntime targetFramework="4.5" relaxedUrlToFileSystemMapping="true" /> to my web.config, and I've also tried adding

<compilation debug="true" targetFramework="4.5">
  <buildProviders>
    <remove extension=".dll"/>
    <remove extension=".exe"/>
  </buildProviders>
</compilation>

And none of it seems to work. So my question is basically, how can I get this URL to work, and map correctly?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could add the following handler to the <handlers> section of your <system.webServer>:

<add 
    name="ManagedDllExtension" 
    path="api/nav/*/*.dll" 
    verb="GET" 
    type="System.Web.Handlers.TransferRequestHandler" 
    preCondition="integratedMode,runtimeVersionv4.0" 
/>

This will make all requests containing .dll be served through the managed pipeline. Also notice how I have limited them only to the GET verb to limit the performance impact.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...