【问题标题】:Deploy ASP.net core 2.1 WEB API to IIS using Visual Studio Code使用 Visual Studio Code 将 ASP.net core 2.1 WEB API 部署到 IIS
【发布时间】:2019-01-06 19:53:56
【问题描述】:

从事 ASP.net core 2.1 Web API 项目。我需要启用 API,以便我们也在开发中的客户端应用程序可以访问它。

到目前为止,我发现发布到 IIS 的唯一方法是执行手动过程:

  • 运行 dotnet publish -c Release
  • 将 bin\Release\netcoreapp2.1\publish\ 中的文件复制到我的 IIS Web App 文件夹中

我想知道是否有更直接的方法可以做到这一点。

此外,构建此版本需要相当长的时间,因此对于开发环境来说,这是一个相当缓慢的过程。问题是在集成测试服务器上使用 F5 运行时,我们不能允许外部访问 WEB api。我们如何实现更敏捷的测试环境?

另一个问题是,当从 javascript 应用程序调用例如 fetch('MyAPIServer/api/MyItems') 时,我收到一个 CORS 错误:

Failed to load http://localhost:86/api/shit: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8082' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled

在开发此类应用程序时,是否绝对需要启用 CORS?

如果我这样获取:

fetch(
    `http://localhost:86/api/shit`,{mode: 'no-cors'}
  )

我明白了:

Uncaught (in promise) SyntaxError: Unexpected end of input
at eval (Pos.vue?7f37:68)

【问题讨论】:

  • 这个问题是我的解决方案,谢谢。在我开发了我的 dot net core webapi 之后,我忘记运行 dotnet publish -c Release thnks
  • 让我补充一点,我们从 VS Code 迁移到 VS Community。 IMO,如果部署到 IIS 是值得的,因为您可以从 GUI 发布

标签: asp.net-web-api asp.net-core asp.net-core-webapi


【解决方案1】:

就 COR 问题而言,您可以将以下内容添加到您的启动中:

public void ConfigureServices(IServiceCollection services)
{
    // Rest of config stuff ...
    services.AddCors();
}

然后你还需要添加以下内容。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseCors(builder =>
            {
                builder.WithOrigins("http://localhost:8080",
                                    "http://localhost:8081", 
                                    "http://localhost:8082")   
                       .AllowAnyMethod()
                       .AllowAnyHeader()
                       .AllowCredentials();
                });
                app.UseMvc();
            }

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 2019-05-23
    • 2022-06-28
    • 2017-02-24
    • 2014-11-16
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多