Introduction to MVC-ASP. NET MVC

Source: Internet
Author: User

1. Definition
Model-View-Controller (MVC) is used to represent a software architecture Model. It divides software systems into three basic parts: models, views, and controllers.

2. Differences between MVC and WebForm Modes

  • WebForm Mode
    A URL request is a physical file in the corresponding path of the server and the URL. The file then processes the request and returns the result to the client.
  • MVC Mode
    The requested URL of the client is mapped to the corresponding Controller. Then, the Controller processes the business logic and may retrieve data from the Model, then, the Controller selects the appropriate View and returns it to the client.

3. directory structure of ASP. net mvc Project

  • App_Data: used to store data
  • Content: used to store resource files (CSS, JS, images, etc)
  • Controllers: used to store the Controller class. The name of the Controller class must end with Controller.
  • Models: class used to store business entities and data access layer code
  • Views: stores view files. Each Controller corresponds to a view folder.
    The Shared sub-directories under the Views directory are used to store Shared Views. For example, Error. aspx and Site. Master

4. ASP. net mvc core DLL

  • System. Web. Routing: URL Routing. Route a URL to the corresponding Controller. It is processed in HttpModule.
  • System. Web. Extensions: ASP. NET AJAX
  • System. Web. Mvc: the main assembly of ASP. net mvc.
  • System. Web. Category actions: some related base classes. For example, HttpContextBase and HttpRequestBase.

5. Routing

  • Add routes to the Application_Start event in the Global. asax file. This ensures that routes is available when the program starts and allows you to call this method directly during unit testing. If you want to call this method directly during unit testing, the method for registering the routes must be static and have a RouteCollection parameter.

    Public static void RegisterRoutes (RouteCollection routes) {// ignore the pair. the Route of the axd file, that is, directly accessing the file like WebForm. axd file routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. mapRoute ("Category", // Route name "Category/{action}/{categoryName}", // URL with parameters new {controller = "Category ", action = "Index", categoryName = "4mvc"} // set the default parameter);} protected void Application_Start () {// register the previously defined Route rule RegisterRoutes (RouteTable. routes );}

6. Controller and Action

  • The Controller class can be divided into the Controller class and the ControllerBase class. The Controller class inherits from the ControllerBase class, while the ControllerBase implements the IController interface.
  • The Action name can be defined and can be defined using ActionNameAttribute.
    public ActionResult Setting(){    throw new NotImplementedException();} [ActionName("Setting")]public ActionResult SaveSetting(){    throw new NotImplementedException();}
  • The parameters of the Action method are the same as those defined in the Route. ASP. net mvc can automatically assign values to parameters of the Action method.
  • The Action method returns the result of the ActionResult type. ASP. net mvc provides the implementation of several ActionResult types, as follows:
    ViewResult. Present the view page to the client. Returned by the View method.
    RedirectToRouteResult. Redirect to another Route. Returned by the RedirectToAction and RedirectToRoute methods.
    RedirectResult. Redirect to another URL. Returned by the Redirect method.
    ContentResult. Common content is returned. For example, a string. Returned by the Content method.
    JsonResult. The JSON result is returned. Returned by the Json method.
    EmptyResult. If Action must return a null value, this result can be returned. No implementation method in Controller. You can return new EmptyResult ().

7. View and ViewData

8. TempData

9. Submit and bind Helper with data

10. ModelState and data verification
  

  
  
  

 

Http://www.cnblogs.com/fish-li/archive/2012/02/12/2348395.html
From: http://www.cnblogs.com/QLeelulu/archive/2008/09/30/1302462.html #
Reference: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.