dotnet core 开发中遇到的问题

2023-05-31,,

1、发布的时候把视图cshtml文件也编译为dll了,如何控制不编译视图?

  编辑功能文件(xx.csproj),加入一个选项:

  <PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>

2、网站里面有 .less等静态资源文件 dotnet core 默认是不允许访问的,如何解决呢

  在startup的Configure方法中加入文件扩展的提供程序,例如:
  

 var Provider = new FileExtensionContentTypeProvider();
Provider.Mappings[".less"] = "text/css";
app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider = Provider
});

3、多cookie登录如何配置呢?

  在startup的 ConfigureServices方法中加入以下代码:

//添加认证Cookie信息
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme + "_client", options =>
{
options.LoginPath = new PathString("/login");
options.AccessDeniedPath = new PathString("/tool");
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.LoginPath = new PathString("/admin/login");
options.AccessDeniedPath = new PathString("/Admin");
});

注意主要使用AuthenticationScheme 区分不同cookie的,所有在登录的时候也要用相应的 AuthenticationScheme,例如:

 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme+"_client");
identity.AddClaim(new Claim(ClaimTypes.Sid, userEntity.LoginName));
identity.AddClaim(new Claim(ClaimTypes.Name, userEntity.LoginName));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme+"_client", new ClaimsPrincipal(identity));

4、多个Areas的站点 发布后在window下可以而linux不行,找不到区域下的视图

  一般是区域的名称设置的大小写的问题,我遇到的问题是 我的程序里面区域是这样定义的  [Area("Admin")],而我发布后区域视图文件夹是 admin 所以找不到

5、dotnet core 命令行启动的时候如何用默认的端或者指定端口呢?

dotnet xx.dll urls="http://*:80"

  

dotnet core 开发中遇到的问题的相关教程结束。

《dotnet core 开发中遇到的问题.doc》

下载本文的Word格式文档,以方便收藏与打印。