【问题标题】:ASP.NET Core 2.2 Web API gives 404 after hosting on IISASP.NET Core 2.2 Web API 在 IIS 上托管后给出 404
【发布时间】:2019-05-28 09:32:04
【问题描述】:

我创建了一个 ASP.Net Core 2.2 Web Api 项目,它在本地运行,没有任何问题。在我将它发布到文件系统后,它总是给我 404 问题。我已经启用了与 IIS 相关的 windows 功能,并且 asp.net 框架 web api2 应用程序在同一台服务器上运行良好。

我启用了 swagger doc 并且也使用了 Microsoft.AspNetCore.Authentication 库。

Program.cs

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace US.BOX.AuthAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

Startup.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using US.BOX.AuthAPI.Extensions;

namespace US.BOX.AuthAPI
{
    public class Startup
    {
        private readonly IConfiguration _configuration;
        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.Configure<IISOptions>(options =>
            {
                options.ForwardClientCertificate = false;
            });

            services.Configure<ApiBehaviorOptions>(options =>
            {
                options.SuppressModelStateInvalidFilter = true;
            });

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton<IAuthenticationSchemeProvider, CustomAuthenticationSchemeProvider>();

            services.AddSwaggerDocumentation();
            services.AddJwtBearerAuthentication(_configuration);

            services.AddCors();
            services.AddLogging();

            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ILogger<Startup> logger)
        {
            app.UseAuthentication();

            if (env.IsDevelopment())
            {
                app.UseSwaggerDocumentation();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

appsettings.json

{
  "JWT": {
    // TODO: This should be updated for production deployment
    "SecurityKey": "sDIkdjhkalUthsaCVjsdfiskokrge",
    "Issuer": "https://{host_name}:{port}",
    "Audience": "https://{host_name}:{port}",
    "ExpirationTimeInMinutes": 60
  },
  "Logging": {
    "LogFilePath": "Logs/auth-{Date}.txt",
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

UsersController.cs

using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace US.BOX.AuthAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetAll()
        {
            try
            {
                return Ok("Users");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

在我发布后,它会生成以下 web.config 文件

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

【问题讨论】:

  • 这个链接可能对你有帮助stackoverflow.com/questions/38529123/…
  • 在 IIS 中选择您的应用程序,然后单击右侧窗格中的浏览链接。那是您尝试使用的 URL 吗?
  • 是的,我已经在 8081 端口下托管了它。所以尝试通过localhost:8081访问它
  • @JanithWidarshana IIS 具有站点和应用程序的概念。如果您不使用此部署替换默认网站,则它不会位于 localhost:port/api/users,而是位于 localhost:port/&lt;application name&gt;/api/users
  • @JanithWidarshana,如果你拿出 app.UseSwaggerDocumentation();在 IF 条件之外,您将在 prod 中拥有 swagger 文档。请看看它是否在IIS中工作。再想一想,你能在getpostman.com 中试试这个localhost:8081/api/users

标签: c# asp.net asp.net-core asp.net-core-webapi


【解决方案1】:

以下是一些您可以检查是否已完成的清单。

  1. 为您的操作系统的 dotnet 核心版本安装 windows-hosting-bundle-installer。您可以从下面的link 下载它
  2. 在您的 IIS 中为 dotnet 核心创建一个新的应用程序池,您可以查看下面的图片以了解设置
  3. 将与 dotnet core 相关的任何应用程序定位到新创建的应用程序池,用于所有托管。

看看以上是否解决了问题。恢复任何查询 如果您的问题得到解决,请投票并点赞,以便对某人有所帮助。

【讨论】:

  • 你能分享你的控制器代码以更好地理解
  • 更新了控制器类的问题。我刚刚从草图创建了 .net core 2.2 web api 项目,没有改变任何东西并试图发布它。它也给出了同样的问题。有人可以试试吗。
【解决方案2】:

希望这会对某人有所帮助。以下是我为解决此问题所做的步骤。

  1. 启用 IIS Hostable Web Core

  2. 安装“Visual C++ Redistributable for Visual Studio 2015”

    https://www.microsoft.com/en-us/download/details.aspx?id=48145

  3. 安装‘dotnet-hosting-2.2.6-win.exe’

    https://dotnet.microsoft.com/download

  4. 在 .NET CLR 版本中使用“无管理代码”创建单独的 IIS 应用程序池

**注意以上步骤的顺序很重要

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 2019-03-18
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2018-09-22
    • 2020-03-09
    • 2020-11-23
    相关资源
    最近更新 更多