ASP.NET MVC 6:https://docs.asp.net/en/latest/mvc/index.html ASP.NET Core :https://docs.asp.net/en/latest/fundamentals/index.html cli-samples : https:/
ASP.NET MVC 6:https://docs.asp.net/en/latest/mvc/index.html
ASP.NET Core :https://docs.asp.net/en/latest/fundamentals/index.html
cli-samples : https://github.com/aspnet/cli-samples
以下是我在學習過程中的一些總結,作此記錄
抱怨!
微軟的發佈候選版本真是坑爹……
1:三月初開始看 ASP.NET Core ,利用 2015 搭建了個測試項目,一切正常一切 OK,可以說是一步到位沒有任何問題(開心得不得了,感覺都快上天了)
2:可惜好景不長,就這本周將項目更新到了RC2,項目的程式包還原就一直報錯:【引用(錯誤-參閱“錯誤列表”)】(失落、悲憤、狂躁……)
3:四處尋覓,終得.NET跨平臺之旅:將示例站點從 ASP.NET 5 RC1 升級至 ASP.NET Core 1.0,喜出望外。
4:今天利用 dotnet restore 更新了包之後又 GG 了。
疑問?
腦子不夠用呀,誰有多的給我來兩斤!
1:CR2 後,如何將項目寄宿到 IIS 或者 IIS Express 中?
2:為何我 2015 中,我右鍵引用-還原程式包總是報錯【引用(錯誤-參閱“錯誤列表”)】,然而在命令行中使用 dotnet restore 之後又正常了。
3:有時候會出現【no actions matched the current request】的錯誤,說什麼路由已經匹配成功了,但是請求匹配不到 action,這又是個什麼梗?但是當我重新修改 Startup.cs 文件之後就又可以了!
…………
N:and so on.
基本配置
時間:2016年3月17日 19:05:03
1,程式入口配置(Program.cs):
public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseServer("Microsoft.AspNetCore.Server.Kestrel") .UseContentRoot(Directory.GetCurrentDirectory()) .UseDefaultConfiguration(args) .UseIISPlatformHandlerUrl() .UseStartup<Startup>() .Build(); host.Run(); } }
註:3.15打假日將
UseApplicationBasePath(Directory.GetCurrentDirectory())
修改為
UseContentRoot(Directory.GetCurrentDirectory())
反正搞不清為什麼,記錄下。
2,啟動項程式(Startup.cs):
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddTransient<Model.Services.StatisticsService>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseMiddleware<artifacts.Middlewares.TimeRecorderMiddleware>(); loggerFactory.AddConsole(LogLevel.Debug); app.UseIISPlatformHandler(); app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.All }); app.UseStaticFiles(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvcWithDefaultRoute(); } }
配置好入口程式和啟動程式後通過命令 dotnet restore 更新包,然後鍵入 dotnet run 開啟自我寄宿服務。
就可以通過 http://localhost:5000 通過預設路由載入頁面。
好了,簡單配置就是這樣啦。
從無建站的簡單流程
1,win + R 鍵入 cmd ,然後定位到一個目錄(我的目錄是D:\ASP.NET)。
D:\ASP.NET>dotnet new Created new C# project in D:\ASP.NET.
2,通過命令 dotnet new 初始化一個簡單基礎的 .net 項目。
D:\ASP.NET>dotnet restore log : Restoring packages for D:\ASP.NET\project.json... info : Committing restore... log : Restore completed in 4200ms. NuGet Config files used: D:\ASP.NET\NuGet.Config C:\Users\Administrator\AppData\Roaming\NuGet\NuGet.Config Feeds used: https://dotnet.myget.org/F/dotnet-core/api/v3/index.json https://api.nuget.org/v3/index.json
3,輸入啟動的命令 dotnet run ,啟動程式。
D:\ASP.NET>dotnet run Compiling ASP.NET for DNXCore,Version=v5.0 Compilation succeeded. 0 Warning(s) 0 Error(s) Time elapsed 00:00:03.2201958 Hello World!
4,就這樣運行起來了,很容易入門呀
//Program 文件 public class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } }
註:目錄結構為:
D:. │ NuGet.Config │ Program.cs │ project.json │ project.lock.json │ ├─bin │ └─Debug │ └─dnxcore50 │ │ ASP.NET.dll │ │ ASP.NET.pdb │ │ │ └─win7-x64 │ ASP.NET.deps │ ASP.NET.dll │ ASP.NET.exe │ ASP.NET.pdb │ hostpolicy.dll │ └─obj └─Debug └─dnxcore50 dotnet-compile-csc.rsp dotnet-compile.assemblyinfo.cs dotnet-compile.rsp
因為 dotnet new 創建的是控制台應用程式,所有就只有一個Program文件。
如果需要搭建 WEB 應用程式,就需要添加 Startup.cs 文件(上面有),然後在 Program.cs 中利用 WebHostBuilder 來寄宿。