.net core创建控制台应用程序和mvc程序

一、创建控制台应用程序

1.查看支持哪些类型:dotnet new --help

2.创建项目(先定位到需要创建的目录)

dotnet new console -o ./myconsole

3.查看目录

 Program.cs内容:

 

4.运行程序,必须进入Program.cs同级目录运行

dotnet run

 

5.上面是Program.cs同级运行的,执行了run之后,我们进入bin目录下会存在如下目录:

在这个目录可以执行:dotnet myconsole.dll。这就是编译后的运行了。

 

二、创建MVC程序

1.创建项目

dotnet new mvc -o ./mymvc

 

2.查看目录

3.运行项目:dotnet run

4.打开一个窗口执行:

         curl http://localhost:5000

  能正常返回说明启动成功。

注意:修改请求的IP和端口。

在Program.cs的同级查看Properties/launchSettings.json文件,没有就创建该文件。launchSettings.json内容如下:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://0.0.0.0:2829/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication1": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://0.0.0.0:2830/"
    }
  }
}

 

里面的端口就是mvc请求端口。里面的IP必须修改为0.0.0.0才能外部访问,否则只能本机访问。

三、通过命令启动

# Unix:
ASPNETCORE_URLS="http://*:5123" dotnet run

# Windows PowerShell:
$env:ASPNETCORE_URLS="http://*:5123" ; dotnet run

# Windows CMD (note: no quotes):
SET ASPNETCORE_URLS=http://*:5123 && dotnet run

 

示例如下: 

SET ASPNETCORE_URLS=http://*:6125 && dotnet GD.NP.ServicesHost.dll
SET ASPNETCORE_URLS=http://*:6200 && dotnet BDCQYTB.Server.ServicesHost.dll

 

四、运行时修改端口

1.通过dos命令运行时修改

我们可以单击exe文件直接运行,直接运行的默认端口是5000,如果我们想要修改,可执行如下命令:

dotnet.exe .\SiteInfo.dll --urls http://*:5009

表示端口修改为5009了。注意:通过dotnet运行只能是dll文件,而不是exe文件

2.通过appsettings.json修改

在appsettings.json中增加如下根节点即可

"urls": "http://*:9099"

 

posted @ 2019-03-14 11:40  段江涛IT  阅读(1555)  评论(0编辑  收藏  举报
页脚HTML代码