简体   繁体   English

参数返回404的WebAPI Route属性

[英]WebAPI Route attribute with parameters returning 404

I can see the appropriate HttpRequest in Fiddler but the Controller method is never invoked but the request seems to be intercepted and a 404 is returned. 我可以在Fiddler中看到适当的HttpRequest ,但是从不调用Controller方法,但是请求似乎已被拦截并返回了404 Other controller methods are invoked appropriately. 其他控制器方法会被适当调用。

HttpRequest HttpRequest的

GET http://localhost:36696/test/file/69946/FF47F87FE63E6C24644631FAEA15B157/file.pdf HTTP/1.1
Host: localhost:36696
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,sv;q=0.6

Controller method 控制器方式

[Route("test/file/{fileId:int}/{hash}/file.pdf")]
public HttpResponseMessage GetFile(int fileId, string hash)
{
    [..]
}

Questions; 问题;

  • How do I properly set the Route ( System.Web.HttpRouteAttribute ) with embedded parameters? 如何正确设置带有嵌入式参数的Route( System.Web.HttpRouteAttribute )?

  • Is there any default filter prohibiting me from accessing PDF files? 是否有任何默认过滤器禁止我访问PDF文件? This is in IIS Express (VS2013) 这在IIS Express(VS2013)中

I cannot post the entire web.config for privacy concerns but there are no httpHandlers or mimeType entries in the file. 为了保护隐私,我无法发布整个web.config ,但是文件中没有httpHandlersmimeType条目。

IIS thinks that it is looking for a static file and is not running the request through the Managed Pipeline. IIS认为它正在寻找静态文件,而不是通过托管管道运行请求。

Adding this entry to the web.config under <handlers> will tell IIS to run these requests through the routes/managed pipeline. 将此条目添加到<handlers>下的web.config中将使IIS通过路由/托管管道运行这些请求。

   <add
        name="ManagedPDFExtension"
        path="test/file/*/*/*.pdf"
        verb="GET"
        type="System.Web.Handlers.TransferRequestHandler"
        preCondition="integratedMode,runtimeVersionv4.0" />

I included the route pretty specifically in the path so that it doesn't run all of your static content through the full .NET pipeline. 我在附带的路线非常明确path ,以便它不通过完整的.NET运行管道所有的静态内容。 If you have lot of these endpoints or serve all files in this way then you can use a more generic path pattern. 如果您有许多此类端点或以这种方式提供所有文件,则可以使用更通用的路径模式。

The issue is the "." 问题是“。” in the route. 在路线上。 The following in the web config will allow it: Web配置中的以下内容将允许它:

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

I should also add that there appear to be other web.config entries that can affect this as well. 我还应该补充一点,似乎还有其他web.config条目也可能会影响到此。 This is just the one I have used to solve this problem. 这只是我用来解决此问题的工具。 A search for "webapi routing with dot" should show you other solutions with deeper explanations as well. 搜索“带点的webapi路由”也应向您显示其他解决方案,并提供更详细的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM