Asp.Net Core在Windows上能夠採用兩種運行方式。一種是自託管運行,另外一種是發佈到IIS託管運行。html
首先有一個無缺的.Net Core WebAPI測試項目,而後進入根目錄運行 dotnet publish ,來進行編譯:json
而後在進入dll目錄,也就是程序集目錄:運行當前項目的主程序dll: dotnet xxx.dllide
出現上面狀況就是完成了,發佈在了5000端口;測試
驗證看一下:ui
.NET Core WebAP默認的端口號是5000,可是咱們能夠經過配置來修改端口號。url
第一步:建立hosting.json文件:spa
{ "server.urls": "http://*:8001;http://*:8002;http://*:8003" }
第二部讀取,並註冊:命令行
public static void Main(string[] args) { //BuildWebHost(args).Run(); var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("hosting.json", optional: true) .Build(); var host = new WebHostBuilder() .UseKestrel() .UseConfiguration(config) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseApplicationInsights() .Build(); host.Run(); }
還有一個就是若是出現一些與:CoreApi.deps.json相關的錯誤,解決辦法:code
找到這個文件而後在裏面添加: <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>server
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest> </PropertyGroup> </Project>
注:若是隻是修改一個端口不作多端口發佈只須要這樣便可:
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseUrls("http://localhost:8002") .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseApplicationInsights() .Build();
還有一種能夠屢次運行命令行來實現不一樣的端口:
首先Program.cs:
public static IWebHost BuildWebHost(string[] args) { var config = new ConfigurationBuilder().AddCommandLine(args) .Build(); string ip = config["ip"]; string port= config["port"]; return WebHost.CreateDefaultBuilder(args) .UseUrls($"http://{ip}:{port}") .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseApplicationInsights() .Build(); }
而後在命令行輸入:
dotnet CoreApi.dll --ip 127.0.0.1 --port 8001