简体   繁体   English

请求 Swagger 返回 404 Not Found

[英]Requests for Swagger return 404 Not Found

This is an ASP.NET MVC application on .NET Core 3.1, with Swashbuckle.AspNetCore 6.1.4 (latest).这是 ASP.NET MVC 应用程序,基于 .NET Core 3.1,带有 Swashbuckle.AspNetCore 6.1.4(最新)。 When I try to load the Swagger UI at /swagger/index.html , I'm getting a 404. It appears that the Swagger middleware isn't handling the request and it's making its way to the static files middleware instead.当我尝试在/swagger/index.html加载 Swagger UI 时,我得到一个 404。看起来 Swagger 中间件没有处理请求,它正在转至 static 文件中间件。 What's infuriating is that this used to work just fine, but I can't figure out what changes I made recently would have caused this problem.令人恼火的是,这曾经工作得很好,但我无法弄清楚我最近所做的哪些更改会导致这个问题。

My Startup.cs file had this (fairly standard):我的 Startup.cs 文件有这个(相当标准):

app.UseSwagger();
app.UseSwaggerUI(options =>
{
    options.RoutePrefix = "swagger";
    options.SwaggerEndpoint($"{SWAGGER_VERSION}/swagger.json", "My API");
    options.EnableDeepLinking();
});
app.UseStaticFiles();

When I noticed the 404 happening, I tried to load the JSON file in my browser, but got the IIS Express 404 page, which had something curious.当我注意到 404 发生时,我尝试在浏览器中加载 JSON 文件,但得到了 IIS Express 404 页面,其中有些奇怪。 Even though the HTTP request was for swagger.json , the error page said:尽管 HTTP 请求是针对swagger.json的,但错误页面显示:

Requested URL: https://localhost:44316/wwwroot/swagger/v1/swagger.js
Physical path: C:\Users\hcheng\source\repos\[xxx]\src\[xxx.yyyy]\wwwroot\swagger\v1\swagger.js

So IIS changed the file extension, and then tried to find a non-existent Javascript file in the wwwroot folder.于是IIS改了文件扩展名,然后试图在wwwroot文件夹中找一个不存在的Javascript文件。 Note that I also have URL rewriting, but none of my rewrite rules are applicable here (I even commented that out entirely, which didn't change anything).请注意,我也有 URL 重写,但我的重写规则均不适用于此处(我什至将其完全注释掉,这并没有改变任何东西)。

I was able to solve this by changing the endpoint to be swaggerjson , which works fine.我能够通过将端点更改为swaggerjson来解决这个问题,效果很好。 However, this didn't resolve the 404 for index.html .但是,这并没有解决index.html的 404 问题。 And for this one, it's something similar:对于这一个,它是类似的:

Requested URL: https://localhost:44316/wwwroot/swagger/index.html
Physical path: C:\Users\hcheng\source\repos\[xxx]\src\[xxx.yyyy]\wwwroot\swagger\index.html

I added some dummy middleware into the pipeline, like so:我在管道中添加了一些虚拟中间件,如下所示:

app.UseSwagger(); // options omitted
app.UseSwaggerUI(); // options omitted
app.Use(async (context, next) => await next()); // breakpoint here
app.UseStaticFiles();
app.Use(async (context, next) => await next()); // breakpoint here

Now when I tried loading /swagger/index.html I hit the first breakpoint, but not the second, which confirms my theory that the Swagger middleware isn't handling the request, and it's the static files one that is doing it instead.现在,当我尝试加载/swagger/index.html时,我遇到了第一个断点,但没有遇到第二个断点,这证实了我的理论,即 Swagger 中间件没有处理请求,而是 static 文件正在处理请求。

UPDATE: It turns out when I hit these breakpoints, the request is actually for favicon.ico ;更新:事实证明,当我遇到这些断点时,请求实际上是针对favicon.ico的; the request for /swagger/index/html doesn't hit these at all, which means IIS is totally bypassing ASP.NET here. /swagger/index/html的请求根本没有命中这些,这意味着 IIS 在这里完全绕过了 ASP.NET。

I really have no idea what I've done to make this not work.我真的不知道我做了什么使它不起作用。 This behavior originally started when I had Swashbuckle 5.6.3 installed, but this persists even after upgrading to 6.1.4.这种行为最初是在我安装 Swashbuckle 5.6.3 时出现的,但即使升级到 6.1.4 后这种情况仍然存在。

This problem has also extended to my ElmahCore implementation, which indicates that it's probably a larger issue.这个问题也扩展到了我的 ElmahCore 实现,这表明它可能是一个更大的问题。 For Elmah, the CSS and JS requests are also 404s and again, it's the static files middleware handling those requests instead of the Elmah middleware.对于 Elmah,CSS 和 JS 请求也是 404,而且,处理这些请求的是 static 文件中间件,而不是 Elmah 中间件。

Any help would be greatly appreciated.任何帮助将不胜感激。

It turns out this wasn't actually a code issue, but a configuration one.事实证明这实际上不是代码问题,而是配置问题。 I was right;我是正确的; there was a recent change that caused the problem.最近有一个变化导致了这个问题。 This got deployed for the first time last week, running in-process in IIS. As such, this requires a web.config file, which I copied from another one of our websites.这是上周第一次部署,在 IIS 中运行。因此,这需要一个 web.config 文件,我从我们的另一个网站复制了该文件。 In that file, it was intercepting requests for static assets (images, stylesheets, HTML files) and serving them directly from IIS, so that meant that the Swagger middleware wasn't even receiving the requests (the same applies to Elmah).在该文件中,它拦截了对 static 资产(图像、样式表、HTML 文件)的请求,并直接从 IIS 为它们提供服务,这意味着 Swagger 中间件甚至没有收到请求(这同样适用于 Elmah)。

<system.webServer>
    <handlers>
        <add name="StaticFileModuleHtml" path="*.html" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
        <add name="StaticFileModuleHtm" path="*.htm" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
        <add name="StaticFileModuleSvg" path="*.svg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
        <add name="StaticFileModuleJs" path="*.js" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
        [etc]

I just had to comment these out and everything was back to normal.我只需要将这些注释掉,一切就恢复正常了。 The Eureka moment came from reading https://learn.microsoft.com/en-us/as.net/core/fundamentals/static-files?view=as.netcore-5.0#serve-files-from-multiple-locations where it states, "If the IIS static file handler is enabled and the ASP.NET Core Module is configured incorrectly, static files are served."尤里卡时刻来自阅读https://learn.microsoft.com/en-us/as.net/core/fundamentals/static-files?view=as.netcore-5.0#serve-files-from-multiple-locations其中它指出,“如果启用了 IIS static 文件处理程序并且 ASP.NET 核心模块配置不正确,则会提供 static 个文件。”

In your ConfigureServices() method (also in Startup.cs) you need this code to set up a Swagger document:在您的ConfigureServices()方法(也在 Startup.cs 中)中,您需要此代码来设置 Swagger 文档:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("{SWAGGER_VERSION}", new OpenApiInfo { Title = "{PROJECT_TITLE}", Version = "{SWAGGER_VERSION}" });
});

Also, I had an issue where I was deploying to IIS and when the Swagger folder was generated, a web.config was generated which was causing the error in my page.此外,我在部署到 IIS 时遇到了一个问题,当生成 Swagger 文件夹时,生成了一个 web.config,这导致我的页面出现错误。

The web.config was generated by my hosting provider. web.config 是由我的托管服务提供商生成的。 In case anyone doesn't know what is wrong, check if a web.config is in your swagger or index.html folders.如果有人不知道出了什么问题,请检查 web.config 是否在您的 swagger 或 index.html 文件夹中。

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

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