【问题标题】:How to Host .net Core 3.0 Web Api on IIS?如何在 IIS 上托管 .net Core 3.0 Web Api?
【发布时间】:2020-03-09 01:22:31
【问题描述】:

我正在尝试通过将 .net core web Api 托管在 IIS 上来在 Remote PC 上运行它。我能够在本地运行应用程序。 Api 在通过 PostMan 运行时工作正常。

我也可以使用 IP 地址运行应用程序

UseUrls("http://localhost:5000", "http://localhost:21868","http://*.*.*.*:5000")

program.cs(使用 kestral 的自托管应用程序)。 在 applicationhost.config

中完成设置
<binding protocol="http" bindingInformation="*:21268:localhost" />

我尝试通过将 API 托管在 IIS 上而不在远程 PC 上进行调试来运行 API,但我无法做到这一点?

编辑:

我在尝试托管时遇到错误-

无法连接到网络服务器 iisexpressandinvalid URl:无法解析主机名。

【问题讨论】:

  • 您遇到的错误是什么?您是否在服务器中安装了 .net 核心托管包?
  • 你检查过Host ASP.NET Core on Windows with IIS吗?进程内 IIS 托管是新项目的默认设置。需要配置IIS,在IIS服务器上install the .NET Core hosting bundle
  • @Shoban 我正在尝试将它托管在 iis 上,但我收到了错误 Unable to connect to web server iisexpressinvalid URl: the hostname could not be parsed
  • @Priya 这看起来更像是你弄错了 eh url。您是在服务器本地检查还是从另一台电脑检查?
  • 请附上屏幕截图。当您说“我正在尝试将其托管在 iis 上”时,您会感到困惑,但错误消息是“无法连接到 web 服务器 iisexpress”。揭示你到底在做什么。

标签: .net asp.net-core iis .net-core-3.0 core-api


【解决方案1】:

缺少的是 - 在设置 Visual Studio 2017/2019 时启用 开发时 IIS 支持,因为在 IIS上运行 ASP.NET Core 应用程序需要本机 IIS 模块>。

之后我可以在项目属性中将启动设置为IIS。我发现下面的链接有助于在 IIS 上托管核心应用程序。

https://devblogs.microsoft.com/aspnet/development-time-iis-support-for-asp-net-core-applications/

【讨论】:

  • 该选项仅在VS2017中可用,您是如何修复2019的?
  • @Norcino 这个选项在 VS2019 中也可用。
【解决方案2】:

编辑您的 Startup.cs 文件

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 = "swagger/{documentName}/swagger.json";
    });

    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.RoutePrefix = "swagger";
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "API 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.UseHttpsRedirection();

    app.UseRouting();

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

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

    app.UseCors();

}   

https://youtu.be/6HiaXCAlRr0

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 2016-04-07
    • 2018-02-17
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2019-07-09
    • 2018-03-18
    相关资源
    最近更新 更多