ASP. net mvc and asp. netmvc

Source: Internet
Author: User

ASP. net mvc and asp. netmvc

MVC (Model-View-Controller, Model-View-Controller mode) is used to represent a software architecture mode. It divides a software system into three basic parts: Model, View, and Controller ).

So what is the difference between the MVC mode and the WebForm mode we are familiar? What is his division of labor?

1. WebForm Request Method

For example, to request a URL (http://www.xmcps.cn/Main/index.aspx), our WebForm program will go down to the root directory of the website to find the index under the Main directory. the aspx file is then created by index. the CodeBehind file (. CS files) for logical processing, including retrieving data from the database (which BLL does not go to DAL here), and then by index. the aspx page is displayed to the user.

Ii. MVC Request Method

 

In MVC, 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. Suppose we run the ASP. net mvc program to access the http://www.xmcps.cn/Home/IndexThis URL actually accessesHomeFor the Index Action in the Controller, see:

WherePublic ActionResult Index ()This method is called Controller'sActionWhich returns the ActionResult type. A Controller can have multiple actions.

How is a URL located in the Controller? Let's take a look at the web. config file. In the httpModules configuration section of the web. config file, we can see a UrlRoutingModule:

<Add name = "UrlRoutingModule" type = "System. Web. Routing. UrlRoutingModule, System. Web. Routing, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>

This UrlRoutingModule locates the URL in the Controller. We can define which Controller the URL will be routed. Let's take a look at the Global. asax file:

We can see that a Route named "Default" is defined here, and the Default parameter is also defined.

The default parameter indicates that when we access a URL such as http: // localhost: 2176/, the parameter does not exist, that is, it is equivalent to accessing http: // localhost: 2176/Home/Index.

Note: When we access the root directory of the website in IIS, if we do not specify the path to access, IIS will access the site based on the default document set in IIS. For example, when we access http: // localhost: 2176/, IIS will find the Default under the root directory of the website. aspx file (assume that the Default file for IIS is set to Default. aspx ). In ASP. net mvc does not process the path of the website root directory such as http: // localhost: 2176/. Therefore, we can see the created ASP. net mvc program has a Default under the root directory. aspx file, which is used to process the previous access to the root directory. Do not delete this file. It submits http: // localhost: 2176/Default. aspx to ASP. net mvc for processing. For details, see the Default. aspx. cs file.

We know how a URL is located in the corresponding Controller, so how is the View returned to the client? We can see from the previous section that the Action method in Controller has a return View () method. By default, the view with the same name as the Action is returned. Under the default view engine (WebFormViewEngine) of ASP. net mvc, the view is accessed in the following path:

/Views/{Controller}/{Action}. aspx

That is, for the http: // localhost: 2176/Home/Index path, when return View () is used in the Index Action by default, will look for/Views/Home/Index. aspx file. If this file cannot be found, it will be searched in the Share Directory:/Views/Share/Index. aspx. If no View is found, an exception occurs. Return View ("lulu. aspx") to specify which view to return:/Views/Home/lulu. aspx.

So why does the 404 error occur when we directly access the files in Views/Home/Index. aspx? In MVC, it is not recommended to directly access the View, so the created ASP. by default, the net mvc program adds a web under the Views directory. the config file contains the following content:

That is to say, access to all the files under the Views directory will be handled by System. Web. HttpNotFoundHandler. Therefore, do not put the resource files (CSS, JS, images, etc.) into the Views directory. If you really want to put it in the Views directory, modify the Views/web. config file.

This article links for your reference: http://www.cnblogs.com/QLeelulu/archive/2008/09/30/1302462.html


Understanding of MVC in ASPNET

The upstairs is wrong, and MVC is not three layers.

In a three-tier architecture, the DAL (data access layer), BLL (business logic layer), and UI (Display) layers perform their respective duties, with the intention of separation of duties.
MVC is the Model-View-Controller. Strictly speaking, the three elements are the display layer in the three-tier architecture. That is to say, MVC splits the UI Layer in the three-tier architecture again, it is divided into three parts: controller, view, and entity. The controller completes the page logic and calls with the interface layer through the entity. The C layer directly communicates with the BLL in the three layers.

How to Implement aspnet mvc?

The problem is widespread in space. It is recommended that you learn mvc first. If you learn about it, you can look at mvc-related projects or the source code of mvc. Good luck! Think more, practice diligently, do not simply look at and do not practice!

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.