ASP. NET MVC Basics

Source: Internet
Author: User

Original address: http://www.cnblogs.com/QLeelulu/archive/2008/09/30/1302462.html

What is the MVC pattern

MVC (Model-view-controller, model-View-controller mode) is used to represent a software architecture pattern.

It divides the software system into three basic parts: model, view, and controller.

So what's the difference between the MVC pattern and our familiar WebForm model? How do the parts of his division work?

Let's look at the normal WebForm mode first. We're asking for a http://www.51mvc.com/blog/. Index.aspx URL, then our WebForm program will go to the site root folder to find the blog folder under the Index.aspx file, and then by the index.aspx page codebehind file (. CS file), which may also include the database to fetch the data (in which case the BLL is not discussed here), which is then presented to the user by the Index.aspx page. Simple examples of the following are seen:

That is, a URL request is a physical file (aspx file or other) on the server with the corresponding path to the URL, which is then processed by the file to process the request and return the result to the client.

But how is this a process for the MVC pattern?

Let's start by building an ASP. NET MVC project. VS2008 The default is no ASP. NET MVC project template, first we need to http://www.microsoft.com/downloads/details.aspx?

familyid=a24d1e00-cd35-4f66-baa0-2362bdde0766&displaylang=en to download the latest ASP. NET MVC installer. Now the latest version of Microsoft ASP Beta (10/15/2008). After the download is installed. We were able to find the ASP. NET MVC project in the new project:


Note: If you are in the Chinese version of VS, after the installation may not find this template phenomenon. You can participate in the Chinese version vs 08 install MVC this article set.

After you build an ASP. NET MVC project, the default items are probably for example:

We can see that there are several directories in the project named and MVC (Model-view-controller. Model-View-controller mode) is appropriate.

Then we take a look at the project:

We notice that the URL of the address bar is Home/index, and we should be able to find the Home folder under the root folder of our project, assuming the WebForm pattern we said earlier. Then there is an index file under the home folder, but we can't find the home folder under the root folder.

Just let us find the views/home/index.aspx file under the Views folder, we enter this address to perform a look:

Oh,no!

The path is right and the file exists, but why is it 404, saying it can't find the file? Assuming that you are not directly visiting the physical files that exist, how does MVC work?

Oh, yes. The working process of the MVC pattern is this:

In MVC, the requested URL of the client is mapped to the corresponding controller, which is then processed by the controller to handle the business logic. You might want to take the data from the model. The controller then chooses the appropriate view to return to the client. Again, the URL of the http://localhost:2176/Home/index that we performed in the ASP. NET MVC program. It is in fact the action of index in the HomeController, see:

Public ActionResult Index () This method is called the controller's Action, and he returns the type of ActionResult.

A controller can have a very multiple action.

So how does a URL be located in the controller? Let's start by looking at the Web. config file in the httpmodules configuration section of the Web. config file. We can see a urlroutingmodule:

<Addname= "UrlRoutingModule"type= "System.Web.Routing.UrlRoutingModule, System.Web.Routing, version=3.5.0.0, Culture=neutral, publickeytoken= 31bf3856ad364e35 "/>


This is the urlroutingmodule to locate the URL to the controller. We are fully customizable to which controller the URL will be routed to. Let's go to the Global.asax file to see:

We can see that a route named "Default" is defined here, and the default number of parameters is defined. The meaning of the default number of parameters is. When we visit a URL like http://localhost:2176/. He will add the non-existent parameters to the default number, which is equivalent to visiting Http://localhost:2176/Home/Index.

Note: We know that in IIS, when we visit the root folder of the site, if we do not specify the path to be visited, IIS will access itself according to the default document set in IIS.

For example, when we visit http://localhost:2176/this URL, IIS will look for the Default.aspx file under the site root folder (if we set the default document for IIS to Default.aspx). In ASP., the path to the site root folder like http://localhost:2176/is not processed by route, so we see a default.aspx file under the root folder of the ASP. NET MVC program we built. This file is used to process the previous access to the root folder. Please do not delete the file. It will be handled by ASP. http://localhost:2176/Default.aspx, see Default.aspx.cs file for details.

We know how a URL is located in the corresponding controller, then how is the view returned to the client? As we can see from the previous section, the action method in the controller has a return View () method. By default, it returns a view with the same name as the action. Under the ASP. NET MVC default view engine (Webformviewengine), view is visited by, for example, the following path:

/views/{controller}/{action}.aspx

That is, for http://localhost:2176/Home/Index this path, by default, when you return to view with return view () in the index action, you will find the/views/home/ index.aspx file. Assume that the file cannot be found. Will go to the Share folder to look for:/views/share/index.aspx, if not found. Throws an exception that cannot be found for the view. Return View ("lulu.aspx") to specify which view:/views/home/lulu.aspx to return.

So why do we go directly to the views/home/index.aspx file here, there will be a 404 error, said the file could not be found? Because in MVC. It is not recommended to visit the view directly. So the ASP. NET-MVC program we built, by default, adds a Web. config file under the Views folder, such as the following:

That is, all the files under the Views folder will be handled by System.Web.HttpNotFoundHandler, so please do not put the resource files (CSS, JS, pictures, etc.) in the Views folder.

Suppose you really want to put it under the Views folder. Please change the Views/web.config file.

At this point, you should have a general understanding of how MVC works.

Let's talk about it first. enjoy!

ASP. NET MVC Basics

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.