使用Visual Studio Code将ASP.net Core 2.1 WEB API部署到IIS

4
在一个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)

https://dev59.com/z1gQ5IYBdhLWcg3wGgBY#43319482 - sideshowbarker
这个问题是我的解决方案,谢谢。在我开发完我的.NET Core WebAPI后,我忘记运行dotnet publish -c Release。谢谢。 - saber tabatabaee yazdi
让我补充一下,我们从VS Code转移到了VS Community。在我的看法中,如果要部署到IIS,这是值得的,因为你可以直接从GUI发布。 - Jack Casas
1个回答

0

关于CORS问题,您可以在启动时添加以下内容:

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();
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接