Pro ASP.NET MVC 5 Reading

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

完整清晰版,完美排版 The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility, combined with all the benefits of ASP.NET. ASP.NET MVC 5 contains a number of advances over previous versions, including the ability to define routes using C# attributes and the ability to override filters. The user experience of building MVC applications has also been substantially improved. The new, more tightly integrated, Visual Studio 2013 IDE has been created specifically with MVC application development in mind and provides a full suite of tools to improve development times and assist in reporting, debugging and deploying your code. The popular Bootstrap JavaScript library has also now been included natively within MVC 5 providing you, the developer, with a wider range of multi-platform CSS and HTML5 options than ever before without the penalty of having to load-in third party libraries. What you’ll learn Gain a solid architectural understanding of ASP.NET MVC 5 Explore the entire ASP.NET MVC Framework as a cohesive whole Learn what's new in version 5 and how best to apply these new features to your own work See how MVC and test-driven development work in action Capitalize on your existing knowledge quickly and easily through comparison of features in classic ASP.NET to those in ASP.NET MV Who this book is for This book is for web developers with a basic knowledge of ASP.NET and C#who want, or need, to start using the new ASP.NET MVC 5 Framework. Table of Contents Part I: Introducing ASP.NET MVC 5 1. What's The Big Idea 2. Your first MVC Application 3. The MVC Pattern 4. Essential Language Features 5. Working with Razor 6. Essential Tools for MVC 7. Sports Store: A Real Application 8. Sports Store: Navigation 9. Sports Store: Cart 10. Sports Store: Mobile 11. Sports Store: Admin 12. Sports Store: Security and Finishing Touches 13. Deployment Part II: ASP.NET MVC 5 In Detail 14. Overview of MVC5 Projects 15. URL Routing 16. Advanced Routing Features 17. Controllers and Actions 18. Filters 19. Controllers Extensibility 20. Views 21. Helper Methods 22. Templated Helper Methods 23. URL and Ajax Helper Meothods 24. Model Binding 25. Model Validation 26. Bundles 27. WebAPI and Single Page Applications
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值