Pro ASP.NET MVC 5 Reading

本文档介绍如何使用 Visual Studio 2017 创建 ASP.NET MVC 项目,并详细讲解了项目的基本结构,包括控制器和视图的创建过程,以及如何使用 HTML Helper 生成 HTML 控件。此外还介绍了设置项目的启动 URL 和 HTTP 请求属性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近来打算新开一个分类,用来写官方原版、外文材料的阅读笔记。正在看的一份文档还没写完,发现之前写过一些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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值