ASP.net MVC 4 Web Api 路由,其中​​包含文件名

标签 asp.net asp.net-mvc asp.net-web-api asp.net-mvc-routing

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

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

负责此 url 的路由如下所示:
        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 错误(下图)。

enter image description here

我试过添加 <httpRuntime targetFramework="4.5" relaxedUrlToFileSystemMapping="true" />到我的 web.config,我也尝试添加
<compilation debug="true" targetFramework="4.5">
  <buildProviders>
    <remove extension=".dll"/>
    <remove extension=".exe"/>
  </buildProviders>
</compilation>

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

最佳答案

您可以将以下处理程序添加到 <handlers>您的 <system.webServer> 部分:

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

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

关于ASP.net MVC 4 Web Api 路由,其中​​包含文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664488/

相关文章:

asp.net - 扩展器不能位于与它扩展 asp.net 的控件不同的 UpdatePanel 中

c# - 如何获取反对派 CsRF 的主机名

asp.net-web-api - 我什么时候应该在 web api 中使用 HttpRequestMessage 作为参数

c# - 如果 ExceptionHandler 捕获异常,则不会调用 DelegatingHandler

asp.net-web-api - "The type IUnitOfWork does not have an accessible constructor"与 Umbraco 6.1、UmbracoApiController(Web API)和依赖注入(inject)(Unity)

c# - 如何创建一个模块以仅允许管理员访问该网站?

ASP.NET Razor Entity Framework 针对模型使用 Case 语句

c# - 如何从 C#code 将 onclientclick 属性添加到 Linkbutton?

css - IIS 7 和字体 awesome .woff 404 错误

c# - 在 CustomValidation 中返回一个复杂对象而不是 ValidationResult