近来打算新开一个分类,用来写官方原版、外文材料的阅读笔记。正在看的一份文档还没写完,发现之前写过一些dotnet系MVC的笔记,这里也po出来,万一突然要搭个dotnet的站点呢
Start Enviroment
We started an Empty MVC project by vs2017.
It generates default folder trees and RouteConfig.cs, Global.asax,Web.config.
Create Empty Controller and View
In Chapter 1, basically, we add controller by the Right-Click menu inControllers folder and choose to add an empty controller.
The generated controller has a default action “Index”, Right-Click the name and add a View. Once we have a Module to bind this view, the option of“Module” in creating a view should not be select by “Empty (Without Module)”.
Use Html Helper to Create HTML controllers
In a cshtml file, with Razor expression, we can render HTML controls by using HTML helper, a C# built-in object. Here is an example:
@Html.DropDownListFor(x => x.WillAttend
,new[] {
new SelectListItem() {Text="Ture", Value=bool.TrueString},
newSelectListItem(){ Text="false", Value = bool.FalseString}}
,"default option")
The “x” in the lambda expression is defined by the statement of view-module binding in the top of the view:
@model MVCEmtyStart.Models.BasicTable
According to this statement, “x” is an object of MVCEmtyStart.Models.BasicTable.
Other methods of HTML Helper is annotated as bellow:
l @Html.ValidationSummary():if the model is invalidated, this method will output the error messages declareby the model's field attribute Required.
l @Href("∼/Content/Site.css"): <linkrel="stylesheet" type="text/css"href="~/Content/Styles.css" />
l @Html.TextBoxFor(x=> x.Name, new { @class = "form-control"}): add class attribute to a html control
Setting the start URL
When press F5 to start the project debug, Visual Studio will, in an effort to be helpful, make the browser request a URL based on the view that is currently being edited. This is a hit-and-miss feature because we cannot start a website form any point in a complex web app. The solution is:
1. Right-Click the project in the Project Browser and select its properties.
2. Select the Web section and check the Specific Page option in theStart Action category. Enter empty value in it, and Visual Studio will requestthe default URL for the project, which is set in RouteConfig.cs
Setting Attributes to Response HTTP request
Attribute bellow tells MVC that this method should be used only for GETrequests. Also, HttpPOST is responsible for POST request.
[HttpGet]
public ActionResultFirstBasicTableForm()
{
return View();
}
Dependency Injection
DI, also known as Inversion of control (IoC)
We don't need to call functions declared in an interface by creating an instance that implements it. To loosely coupled components, we may declare an object with the specific interface statement in its constructor argument. So far, in our class, we don't need to know how to instantiate an instance implemented specific interfaces.
But outside the class above, when we instantiate it, an implementation should be passed to the constructor. This means a new object should be created before. DI container can do a mapping between interfaces and class. When we want an object constructed with an implementation, ask the DI container for creatingone for us. Sadly, a reliable, robust and high-performance DI container is not easy to build. Ninject is a recommended DI container.
reference: Pro ASP.NET MVC 5 written by Adam Freeman