ASP.net MVC4 WebApi 路由,其中​​包含文件名 [英] ASP.net MVC4 WebApi route with file-name in it

查看:26
本文介绍了ASP.net MVC4 WebApi 路由,其中​​包含文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让以下(和类似的)网址在我的 ASP.net MVC4/WebApi 项目中工作:

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

负责这个 url 的路由看起来像这样:

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 }
        );

如果我删除 .在文件名中,或者如果我在 URL 后面添加一个斜杠,但这也意味着我不能使用 Url.Route-methods 等.我得到的错误是一个通用的 404 错误(下图).

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).

我尝试将 <httpRuntime targetFramework="4.5" RelaxUrlToFileSystemMapping="true"/> 添加到我的 web.config,并且我也尝试添加

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>

而且似乎都不起作用.所以我的问题基本上是,我怎样才能让这个 URL 工作,并正确映射?

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

推荐答案

您可以将以下处理程序添加到 部分代码>:

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" 
/>

这将使所有包含 .dll 的请求通过托管管道提供服务.还要注意我是如何将它们限制在 GET 动词上以限制性能影响的.

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.

这篇关于ASP.net MVC4 WebApi 路由,其中​​包含文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆