• ASP.NET Core 使用记录1


    ASP.NET 项目启动 提示 ID为XXX的进程未启动

    原因:暂时不能明确。

    解决方案:

    删除项目的 csproj 文件的WebProjectProperties节点内容。

            <WebProjectProperties>
              <UseIIS>True</UseIIS>
              <AutoAssignPort>True</AutoAssignPort>
              <DevelopmentServerPort>0</DevelopmentServerPort>
              <DevelopmentServerVPath>/</DevelopmentServerVPath>
              <IISUrl>http://localhost:55990/</IISUrl>
              <NTLMAuthentication>False</NTLMAuthentication>
              <UseCustomServer>False</UseCustomServer>
              <CustomServerUrl>
              </CustomServerUrl>
              <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
            </WebProjectProperties>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ASP.NET CORE 监听地址

    在将项目部署到云服务器上时,在云服务器启动项目服务时,默认的 launchsetting.json 里的applicationUrl是监听 http://localhost:5000 这些url,而我们要想通过公网访问我们的接口服务,这样的配置是不行的。
    需要改成 http://*:5000 这样的url,才能通过公网IP来访问我们的项目服务。
    ASP.NET Core 设置urls
    其中设置url的优先级问题:Kestrel > 命令行 > 配置文件 > UseUrls > 环境变量 > 默认值

    1. kestrel 配置
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                // 配置Kestrel服务
                webBuilder.UseKestrel(kestrelServerOptions =>
                {
                    kestrelServerOptions.ListenLocalhost(7004);
                    kestrelServerOptions.ListenLocalhost(7014, listenOptions => listenOptions.UseHttps());
                });
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 命令行配置
    dotnet AspNetCoreUrl.dll --urls "http://localhost:7001;https://localhost:7011"
    
    • 1
    1. 配置文件

    在生成程序的根目录下,打开appsettings.json文件,添加url配置项

    {
        "urls":"http://localhost:7002;http://localhost:7012"
    }
    
    • 1
    • 2
    • 3
    1. useurls 配置
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                // 使用UseUrls设置监听的端口和协议
                webBuilder.UseUrls("http://localhost:7003", "https://localhost:7013");
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 环境变量配置

    在不修改AspNetCoreUrl任何源代码的情况下(即创建项目时的程序默认状态)生成程序,定位到生成的根目录下,打开命令行终端
    在这里插入图片描述

    # 环境变量仅在当前命令行窗口生效
    $Env:ASPNETCORE_URLS = "http://localhost:7000;https://localhost:7010"
    # 或者使用DOTNET_URLS环境变量同样可生效
    $Env:DOTNET_URLS = "http://localhost:8000;https://localhost:8010"
    # 运行AspNetCoreUrl程序
    dotnet AspNetCoreUrl.dll
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如果使用Windows命令行(即cmd命令行),使用下面的方式设置

    # 环境变量仅在当前命令行窗口生效
    set ASPNETCORE_URLS=http://localhost:7000;https://localhost:7010
    # 将ASPNETCORE_URLS变量保存到用户环境变量中
    setx ASPNETCORE_URLS "http://localhost:7000;https://localhost:7010"
    # 加/m参数,将ASPNETCORE_URLS变量保存到系统环境变量中
    setx ASPNETCORE_URLS "http://localhost:7000;https://localhost:7010" /m
    # 运行AspNetCoreUrl程序
    dotnet AspNetCoreUrl.dll
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    注意:使用setx设置环境变量后,需要打开新的Windows命令行窗口才会使用环境变量生效
    在Linux系统中使用以下命令设置环境变量

    # 环境变量仅在当前终端生效,关闭终端后需要重新设置 
    export ASPNETCORE_URLS="http://localhost:7000;https://localhost:7010"
    
    • 1
    • 2
    1. 默认值

    默认值就是默认监听的 5000 和 5001端口号.

  • 相关阅读:
    移动端ViT新利器!苹果提出稀疏专家混合模型Mobile V-MoEs
    【AI】推理系统和推理引擎的整体架构
    Spring常见问题解决 - this指针造成AOP失效
    计算机毕业设计 基于SpringBoot+Vue的财务管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
    PPT系统化学习 - 第1天
    【Linux】 - linux文本编辑器vim的常用操作
    java面试小经历
    《C++ Primer Plus》第九章:内存模型和名称空间(1)
    高企认定人员及研发费要求?
    Java项目硅谷课堂学习笔记-P8点播模块管理-后台-管理员端
  • 原文地址:https://blog.csdn.net/weixin_46178278/article/details/125584360