捕获所有路由不捕获静态文件

时间:2016-05-06 17:54:30

标签: c# asp.net asp.net-web-api

我正在尝试使用ASP.Net Web Api 2创建反向代理,使用此示例:http://kasperholdum.dk/2016/03/reverse-proxy-in-asp-net-web-api/

那里最重要的两件事情是:

  1. 添加DelegatingHandler:config.MessageHandlers.Add(new ProxyHandler());
  2. 添加一个包罗万象的路线:config.Routes.MapHttpRoute(“abe”,“{* path}”);
  3. 在我调试时,它对基本URL(http://localhost:51101/)工作正常,但不会重定向对静态文件的引用:

    http://localhost:51101/logos/doodles/2016/sigmund-freuds-160th-birthday-4918124856999936-hp.jpg

    如果我使用该网址,我会收到此错误:

    HTTP Error 404.0 - Not Found
    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
    
    Most likely causes:
    The directory or file specified does not exist on the Web server.
    The URL contains a typographical error.
    A custom filter or module, such as URLScan, restricts access to the file.
    
    Things you can try:
    Create the content on the Web server.
    Review the browser URL.
    Check the failed request tracing log and see which module is calling SetStatus. For more information, click here.
    
    Detailed Error Information:
    Module     IIS Web Core
    Notification       MapRequestHandler
    Handler    StaticFile
    Error Code     0x80070002
    Requested URL      http://localhost:57221/logos/doodles/2016/sigmund-freuds-160th-birthday-4918124856999936-hp.jpg
    Physical Path      c:\users\...\documents\visual studio 2015\Projects\AspNetReversePRoxy2\AspNetReversePRoxy2\logos\doodles\2016\sigmund-freuds-160th-birthday-4918124856999936-hp.jpg
    Logon Method       Anonymous
    Logon User     Anonymous
    Request Tracing Directory      C:\Users\...\Documents\IISExpress\TraceLogFiles\ASPNETREVERSEPROXY2
    
    More Information:
    This error means that the file or directory does not exist on the server. Create the file or directory and try the request again.
    View more information »
    

    问题

    我在网址路由中犯了错误吗? 有没有办法可以捕获静态文件?那么处理程序可以拦截调用吗?

1 个答案:

答案 0 :(得分:4)

为了处理静态文件,您需要将以下内容添加到配置文件中:

<configuration>
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
   </system.webServer>
</configuration>

否则,对于静态文件的请求,将不会运行托管模块。

相关问题