分类 C# 下的文章

有时为了让前端更方便的修改 cshtml 文件,又不想在前端电脑上安装开发环境,就想打包出来,view 还是像老版本的 mvc 一样独立怎么办呢,很简单3步搞定:

1、修改 .csproj 项目文件:

<PropertyGroup>
    <!--不打包视图文件-->
    <RazorCompileOnBuild>false</RazorCompileOnBuild>
    <RazorCompileOnPublish>false</RazorCompileOnPublish>
</PropertyGroup>

2、添加 nuget 包引用:Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
3、修改 Program.cs:

builder.Services.AddRazorPages().AddRazorRuntimeCompilation();

需要在 Program.cs (or Startup.cs) 中增加:
builder.Services.AddControllers();

var app = builder.Build();

app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

添加 API 目录,添加 WebApi 控制器,如果不能访问:
在 App_Start 目录下添加 WebApiConfig.cs:

using System.Web.Http;

namespace pc530.App_Start
{

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

}

Global.asax 中,Application_Start 添加一行:
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

安装net 6.0环境几种方式:

一、直接用源安装

On Ubuntu 20.04

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb

On Ubuntu 18.04

wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb

开发SDK

apt update
apt install -y apt-transport-https
apt install -y dotnet-sdk-6.0

应用程序运行时

apt update
apt install -y apt-transport-https
apt install -y aspnetcore-runtime-6.0

ASP.NET Core Runtime

apt update
apt install -y apt-transport-https
apt install -y dotnet-runtime-6.0

二、官方脚本

wget https://dot.net/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh -c 6.0

增加变量:
$ vim ~/.bashrc
export PATH=$PATH:$HOME/.dotnet
export DOTNET_ROOT=$HOME/.dotnet
source ~/.bashrc

三、用 Snap 安装

apt install snapd
systemctl enable --now snapd.socket
systemctl enable --now snapd apparmor
snap install dotnet-sdk --classic --channel=6.0
snap alias dotnet-sdk.dotnet dotnet
snap install dotnet-runtime-60 --classic
snap alias dotnet-runtime-60.dotnet dotnet
vim ~/.bashrc
export DOTNET_ROOT=/snap/dotnet-sdk/current
source ~/.bashrc

开启 Tab 自动完成功能:

$ vim ~/.zshrc

# zsh parameter completion for the dotnet CLI
_dotnet_zsh_complete()
{
  local completions=("$(dotnet complete "$words")")
  reply=( "${(ps:\n:)completions}" )
}
compctl -K _dotnet_zsh_complete dotnet

$source ~/.zshrc

$ vim ~/.bashrc

# bash parameter completion for the dotnet CLI
_dotnet_bash_complete()
{
  local word=${COMP_WORDS[COMP_CWORD]}

  local completions
  completions="$(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)"
  if [ $? -ne 0 ]; then
    completions=""
  fi
  COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -f -F _dotnet_bash_complete dotnet

$ source ~/.bashrc

测试:

dotnet new console --output testapp1
dotnet run --project testapp1

打包时,选择 linux

安装SVN:

apt install subversion
svn checkout https://server/myproject
cd myproject
chmod 777 ./myproject

后台运行

nohup ./myproject> /dev/null 2>&1 &

web 项目后台运行:

nohup dotnet Demo.Net.Core.dll --urls="http://:8081;http://:8082" --environment=Delopment > /dev/null 2>&1 &

注意,后台运行时,不能用 ReadKey, ReadLine 之类的方法,会导致一行动就出错退出。