IIS 重写,swagger ui 给出 404 未找到错误

问题描述 投票:0回答:1

我正在使用 Vue js 应用程序托管 Dotnet Core 3.1 项目。为此,我将 vue js 静态文件托管在 www-root 文件夹中。

我的应用程序中也有一个 swagger UI。

这是我在托管目录中的默认 Web 配置。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" 
                stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>

使用此配置,swagger ui 运行良好,没有任何错误。

但是我必须为 Vue js 应用程序添加重写逻辑,因此我使用以下配置更新了 Web 配置。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="wwwroot-static" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|js|css|png|gif|jpg|jpeg))" />
          <action type="Rewrite" url="wwwroot/{R:1}" />
        </rule> 

        <rule name="empty-root-index" stopProcessing="true">
          <match url="^$" />
          <action type="Rewrite" url="wwwroot/index.html" />
        </rule>

        <!-- 
             Make sure you have a <base href="/" /> tag to fix the root path 
             or all relative links will break on rewrite 
        -->
        <rule name="AngularJS-Html5-Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/.well-known/openid-configuration" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/.well-known/jwks" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/api(.*)" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/swagger(.*)" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/account(.*)" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/connect(.*)" negate="true" />
          </conditions>
          <action type="Rewrite" url="wwwroot/index.html"  />
        </rule> 
      </rules>
    </rewrite>

    <handlers>
      <add name="StaticFileModuleHtml" 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" />
      <add name="StaticFileModuleCss" path="*.css" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
      <add name="StaticFileModuleJpeg" path="*.jpeg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
      <add name="StaticFileModuleJpg" path="*.jpg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
      <add name="StaticFileModulePng" path="*.png" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
      <add name="StaticFileModuleGif" path="*.gif" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" 
                stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>

通过 Web 配置中的此配置,我的 vue js 应用程序运行良好。但是当我访问网站:http(s)://[MYWEBAPP]/swagger 时,它会重定向到 http(s)://[MYWEBAPP]/swagger/index.html 并给出 404 文件未找到错误。

我的启动类中的配置服务如下所示:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseSwagger();
    app.UseSwaggerUI(c =>
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API")
    );

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
        
    app.UseHttpsRedirection();
    
    app.UseStaticFiles();
    app.UseRouting();
    
    app.UseAuthentication();  
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
       endpoints.MapControllerRoute(
       name: "default",
       pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

尝试了多个可用的解决方案,我意识到 swagger UI 正在尝试从 wwwroot 中查找 index.html,因此导致了未找到错误。我不知道如何建议 swagger UI 来查看自己的目录。

这可能存在什么问题?我在这里缺少什么?其他人也可能有这个问题吗?

swagger-ui asp.net-core-3.1 url-rewrite-module
1个回答
0
投票

我的 React 应用程序也遇到了同样的问题。要将 swagger 请求传递给 .Net 应用程序,IIS 必须执行以下操作:

  1. 不重写以
    swagger
  2. 开头的 URL
  3. 根据其
    path
    属性选择正确的处理程序

我的 web.config 看起来与此 xml 类似。重要的是首先重写

swagger/*
路径的规则和处理程序。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <rewrite>
        <rules>
          <rule name="swagger" stopProcessing="true">
            <match url="swagger/.*" />
          </rule>

          <rule name="HTTPS force" enabled="false" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
          </rule>

          <rule name="wwwroot-static" stopProcessing="true">
            <match url="([\S]+[.](.+))" />
            <action type="Rewrite" url="wwwroot/{R:1}" />
          </rule>

          <rule name="empty-root-index" stopProcessing="true">
            <match url="^$" />
            <action type="Rewrite" url="wwwroot/index.html" />
          </rule>

          <rule name="React-Html5-Routes" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                            <add input="{REQUEST_URI}" pattern="api/(.*)" negate="true" />
            </conditions>
            <action type="Rewrite" url="wwwroot/index.html" />
          </rule>
        </rules>
      </rewrite>

      <handlers>
        <add name="aspNetCoreSwagger" path="swagger/*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
        <add name="StaticFileModule" path="*.*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>

      <aspNetCore processPath="dotnet" arguments=".\API.dll" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>
© www.soinside.com 2019 - 2024. All rights reserved.