1.路由
控制器添加特性: [RoutePrefix("api/controller")] = > [Route("api/[controller]")]
方法添加特性:
[HttpGet]
[Route("{id}")] => [HttpGet("{id}")]
2.返回參數
webapi IHttpActionResult
mvc ActionResult => IActionResult
3.數據綁定(沒變化)
4.驗證(沒變化)
if (!ModelState.IsValid)
{
}
5.依賴注入,可以在視圖使用
@inject SomeService ServiceName <!DOCTYPE html> <html> <head> <title>@ServiceName.GetTitle</title> </head> <body> <h1>@ServiceName.GetTitle</h1> </body> </html>
6.Tag Helpers(一般使用asp-前綴)
// a標簽綁定 /Account/Login
<p> Thank you for confirming your email. Please <a asp-controller="Account" asp-action="Login">Click here to Log in</a>. </p>
7.EnvironmentTagHelper 可以根據運行環境的不同產生不同的html代碼
<environment names="Development"> <script src="~/lib/jquery/dist/jquery.js"></script> </environment> <environment names="Staging,Production"> <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js" asp-fallback-src="~/lib/jquery/dist/jquery.min.js" asp-fallback-test="window.jQuery"> </script> </environment>
8. layout pages 布局頁
aspnetcore mvc 布局頁也有所改變,默認模版頁在~/Views/_ViewsStart.cshtml 被設置,如下,設置~/Views/Shared/_Layout.cshtml為默認模板頁
@{
Layout = "_Layout";
}
若想不使用模版頁,則在視圖設置Layout=null,或者設置為其他模版頁
9. title設置
aspnet mvc : ViewBag.Title=xxxx
aspnetcore mvc : ViewData["Title"] = "xxx"(需要在模版頁的title中引用ViewData["Title"])
一些方法:
HtmlEncoder.Default.Encode(string value) 防止js注入