当前位置: 首页 > 知识库问答 >
问题:

ASP.NET 核心 5.0 Web API 适用于 IIS 速成,但在 IIS 10 上承载时会给出 404

滕学义
2023-03-14

我在单个解决方案中有一个WEB APP和一个WEB API项目。最近,我们在API项目中添加了Swashbuckle和Swagger UI。现在,当使用IIS Express Swagger在VS中运行表单(调试/不调试)时,Web API工作正常。json可访问,Swagger UI可用https://localhost:/API/docs/index.html然后尝试一下https://localhost:/API/docs/,返回有效输出。

但是,在发布并部署到IIS站点后,它会返回404错误。招摇过市。json已形成,Swagger UI已正确提供https://domain:/API/docs/index.html但在尝试时https://domain:/API/docs/以404响应。(DNS映射正确)。

IIS 10.0详细错误-404.0-未找到

Detailed Error Information: 
Module        IIS Web Core 
Notification MapRequestHandler 
Handler       StaticFile 
Error Code   0x80070002 
Requested URL https://localhost:<port>/Rejected-By-UrlScan?~/API/docs/<command> 
Physical Path <path>iis\abc\Rejected-By-UrlScan 
Logon Method Anonymous 
Logon User      Anonymous

通过CLI成功发布

dotnet publish -c release -r win-x64 --self-contained false

启动.cs

  public void ConfigureServices(IServiceCollection services)
            {
            
 
 
 
 
 
 
 
 services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
            .AddCertificate();

            services.Configure<ApplicationSetting>(Configuration.GetSection("ApplicationSettings"));
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                    .AllowAnyMethod()
                                                                     .AllowAnyHeader()));


            services.AddControllers()
                    .ConfigureApiBehaviorOptions(options =>
                    {
                        options.SuppressModelStateInvalidFilter = true;
                    });


            services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
                    .AddNewtonsoftJson(options =>
                    {
                        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    });

            services.AddMvcCore()
                    .AddApiExplorer();

            abcDBContext.ConnectionString = Configuration.GetConnectionString("abcDBContext");

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = API",
                    Description = "Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "ABC Support",
                        Email = string.Empty,
                        Url = new Uri("https://example.com/"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            DependencyInjections.Dependency(services,Configuration);
             
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            if (env.IsProduction() || env.IsStaging())
            {
                app.UseExceptionHandler("/Error/index.html");
            }

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger(c =>
            {
                c.RouteTemplate = "docs/{documentName}/swagger.json";
            });

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.RoutePrefix = "docs"; 
                c.SwaggerEndpoint("v1/swagger.json", "v1");

                // custom CSS
                c.InjectStylesheet("/swagger-ui/custom.css");
            });

            app.Use(async (ctx, next) =>
            {
                await next();
                if (ctx.Response.StatusCode == 204)
                {
                    ctx.Response.ContentLength = 0;
                }
            });


            app.UseCors(builder =>
            builder.WithOrigins(Configuration["ApplicationSettings:Client_URL"].ToString())
            .AllowAnyOrigin()
            .AllowAnyHeader()
            .AllowAnyMethod()
            );

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();
            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseCors();

        }

LaunchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:2618",
      "sslPort": 44311
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "docs",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Web.API": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "docs",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

操作系统 : 视窗 10 最新版本 x64

IIS站点(使用相同的应用程序池)

应用程序池详细信息

IIS网站

共有1个答案

马沛
2023-03-14

我通过卸载URLScan 3.1解决了这个问题,基于Lex Li分享的链接

 类似资料:
  • .NET核心和ASP.NET核心到底有什么区别?

  • 在IIS中,我完成了以下步骤: 创建了一个“无托管代码”和“集成管道”的新AppPool 我们正在使用“身份”下的服务帐户 创建了一个新的Web应用程序 “启用”Windows身份验证,并将协商作为主要证明程序 匿名身份验证“已禁用” system.webserver/security/authentication/WindowsAuthentication“useAppPoolCredentia

  • . NET Core gRPC-Web客户端调用ASP.NETCore gRPC-Web服务器http://localhost:5000正常工作。 与部署到具有虚拟应用程序的IIS服务器的客户端代码调用服务器相同(例如http://build.mycompany.ca/myapp)的结果 状态(StatusCode="未实现",Detail="错误的gRPC响应。HTTP状态代码:404") 我的

  • 我正在使用ASP.NET内核。我正在创建一个基本的WebAPI。我想在出现问题时显示一个JSON错误。 打印屏幕在我的屏幕上显示want I want。唯一的问题是它的发送状态码为200。 我可以通过这样做来解决问题: 真诚的,布莱希特

  • 任何人都不知道,在ASP.NET C#中如何从客户端获取X509Certificates证书,并且在IIS服务器上托管的这个应用程序而不是控制台应用程序。 我的参考代码:

  • 例如,用谷歌搜索网站,登陆主页,然后进入跨域网站正确记录“有机”作为媒介 (已通过在GA中使用实时数据进行验证) 但是,通过谷歌搜索网站,登陆主页,然后进入跨域网站并完成一个事件,会错误地将“Direct”记录为该事件的媒介。 GTM设置已配置为: -5个域推送到“汇总”GA帐户 -autoLinker=true -Cookie-Domain=auto -auto-Link-domains=sit