.Net Core開發可以使用Visual Studio 2017或者Visual Studio Code,下面使用Visual Studio 2017搭建一個.net Core MVC網站項目。
一.新建項目
二.選擇 Web應用程序(模型視圖控制器)
三.項目結構和之前的比對還是有很大的不同,wwwroot用來存放前端的一些靜態資源(css/js/image/html),以前是通過Nuget來下載前后端包,現在前端包使用Bower下載,后端的包的使用Nuget下載
四.運行
.net core項目調試默認使用IIS Express,對比之前的網站項目多了一個控制台方式
控制台方式,會顯示一些調試信息:
另外注意:
1。.Net Core中WebApi由以前的繼承ApiController改為了Controller,與Mvc共用一個Controller
2。.Net Core中還是可以引用之前版本的程序集,可以共用,也不是一定需要使用Nuget包下載(會有兼容性問題)
3。過濾器還是可以使用ActionFilterAttribute/ExceptionFilterAttribute,不過命名空間改為了 Microsoft.AspNetCore.Mvc.Filters,在Startup類中添加,使用:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.Filters.Add(typeof(SimpleActionFilterAttribute)); options.Filters.Add(typeof(SimpleExceptionFilterAttribute)); }); }
4。在MVC視圖中可以使用這樣一種方式,自動添加版本號(js/css/img等靜態資源)[ 修改其中代碼則會自動的改變版本號],寫法(asp-append-version=“true”):
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />