asp-发布

IIS

Install the ASP.NET Core Module/Hosting Bundle

前后端独立部署

后端API 和 前端程序分开部署,对于前后端没有任何限制。

此方法的限制:需要给前端和后端分配单独的域名,具有跨域问题需要配置跨域,因为有跨域,在调用API时还有会额外的 HTTP OPTIONS 请求。

Kestrel

此方法是将前端项目发布后,Copy 到后端 WebApi 项目下的 wwwroot 目录下(没有就新建),让 Kestrel 来同时提供 api 和 前端静态资源服务,适合内部使用小型项目,不建议用在中大型项目。

此方法的限制:前端必须使用基于 hash 的路由方式,基于 history 的不行;后端 WebApi 项目需要添加静态文件中间件和默认文件中间件
这样进行发布后,即便是打包成 Docker 镜像也只需要一个,比较方便。

此方法部署没有跨域问题,后端无需配置跨域,没有额外的 HTTP OPTIONS 请求。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseDefaultFiles();
app.UseStaticFiles();
}

Nginx 全代理法

此方法是 nginx 根据请求路径来指向前端资源或者代理后端 api,和上面的方法一样,也只使用一个域名,没有跨域问题、

此方法的限制:后端必须设置给 api 设置统一的前缀。

api 的前缀,是自定义的,一般以 api作为前缀,例如:/api/apple/add。

最好本地vs发布环境和服务器上的core环境相一致。vs 发布环境安装 SDK3.1.2。IIS服务器上安装 core Runtime 3.1.2和Hosting Bundle 3.1.2 这2个安装包。(注意,如果iis服务器环境安装了以前版本的core,比如说3.0,发布后可能会有500错误。删除旧版本统一新版本解决

https://dotnet.microsoft.com/en-us/download/dotnet/3.1

安装IIS和Windows Server Hosing之后 ,重启IIS服务,检查IIS “Modules” 中是否有 “AspNetCoreModuleV2”
Application pool 设置 No Managed Code, pipe line mode: integrated
advanced settings: 32bit-application: false

Linux

  • Nginx
  • Apache
    Windows
  • IIS
  • Windows Service

The Kestrel web server is a new web server as part of ASP.NET Core.
It is now the preferred web server for all new ASP.NET applications.

it is still recommended to use IIS, Apache, or NGINX as a reverse proxy in front of it.

20220720_113459.png

Q&A

System.UnauthorizedAccessException: Filename: redirection.config
Error: Cannot read configuration file due to insufficient permissions

THE SOLUTION
The service account for the IIS App Pool does not have the required read rights on folder %windows%\system32\inetsrv\config.

Therefore, You must grant the account Read rights on this folder and then recycle the App Pool in use for the Log API. As an alternative, you should make sure the account is part of the local IIS_IUSRS group and then assign this group read rights on the folder with the redirection.config file.