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=null,或者设置为其他模版页
9. title设置
aspnet mvc : ViewBag.Title=xxxx
aspnetcore mvc : ViewData["Title"] = "xxx"(需要在模版页的title中引用ViewData["Title"])
一些方法:
HtmlEncoder.Default.Encode(string value) 防止js注入