DotNetCore跨平臺~Startup類的(de)介(jie)紹
新寵兒
DotNetCore是(shi)(shi).net5.0版本,之所以(yi)不(bu)叫.net5.0為(wei)的就是(shi)(shi)不(bu)讓(rang)我們把它與前面的.net混為(wei)一(yi)淡,它將是(shi)(shi)真正意義的跨(kua)平臺開發(fa)語言,在網(wang)上也(ye)有(you)相關介(jie)紹(shao),中國的一(yi)些大牛(niu)(niu)也(ye)發(fa)了相關文(wen)章,像(xiang)張善友大牛(niu)(niu)也(ye)發(fa)了幾個文(wen)章,來介(jie)紹(shao).NetCore,這段時間,大叔將會一(yi)步(bu)(bu)一(yi)步(bu)(bu)說說這個跨(kua)平臺的新(xin)寵兒!
重新起名了
- ASP.NET 5 –> ASP.NET Core 1.0
- .NET Core 5 –> .NET Core 1.0
- Entity Framework 7 –> Entity Framework Core 1.0
起航
下載.net core 1.0.0 rc2版
建立一個MVC項目
之前我們會看到相應的信息,今天主要說(shuo)一下(xia)起始(shi)文件,Startup.cs
Configure方法主要作用是對運行環境進行個性化配置,如調試環境(Development),生產環境(Production),跌代環境(Staging)等
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); //判斷運行環境 if (env.IsDevelopment()) { // For more details on using the user secret store see //go.microsoft.com/fwlink/?LinkID=532709 builder.AddUserSecrets(); // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. builder.AddApplicationInsightsSettings(developerMode: true,endpointAddress:"localhost:5000"); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); }
需(xu)要希望修(xiu)改監聽的(de)端口,需(xu)要修(xiu)改program.cs程序(xu),具(ju)體如下(xia)
public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .UseUrls("//192.168.2.22:5000") .Build(); host.Run(); host.Run(); }
這(zhe)樣我們的第一個應(ying)用程序就(jiu)可以(yi)了,它(ta)可以(yi)部署到linux,maxos等操(cao)作系統上(shang),只(zhi)需要安裝(zhuang)dotnet-cli即可。
終于OK了(le),咱們的(de)第一(yi)個跨(kua)平臺的(de).net程序完成(cheng)了(le)!
謝謝您的閱讀!