Introduction to ASP. NET MVC 1

Source: Internet
Author: User

Introduction to ASP. NET MVC 1

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 various parts of his division work?

Let's take a look at the normal WebForm mode, we ask for an example http://www.51mvc.com/blog/ Index.aspx URL, then our WebForm program will go to the root directory of the site to look for the index.aspx file under the blog directory, and then by the index.aspx page codebehind file (. CS file), which may include a database to fetch data (where the BLL is not discussed here), and then be presented to the user by the Index.aspx page. It is simple as follows:

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 returns the result to the client.

But what is this 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, currently the latest version of Microsoft ASP . NET MVC Beta (10/15/2008). After the download is installed, we can 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 refer to the Chinese version vs 08 installation of MVC this article set.

When an ASP. NET MVC project is established, the default project is probably:

We can see that there are several folders in the project named and MVC (Model-view-controller, model-View-controller mode) are corresponding. Then we run the project to see:

We notice that the URL of the address bar is Home/index, if you follow the WebForm pattern we said earlier, we should be able to find the home directory under the root of our project, and then the home directory has an Index file, But we are not able to find the home directory under the root directory. However, let us in the views directory to find the views/home/index.aspx file, we enter this address to run to see:

Oh,no! The path is right and the file exists, but why is it 404, saying it can't find the file? If you do not have direct access to the physical files that exist, how does MVC work?

So, the working process of the MVC pattern is this:

In MVC, the requested URL of the client is mapped to the appropriate controller, then the controller handles the business logic, perhaps taking the data from the model, and then the controller chooses the appropriate view to return to the client. And again back in front of our run ASP. http://localhost:2176/home/index This URL, which is actually accessed by Home The action of the index in the controller, see:

Where public actionresult Index () This method is called the controller's Action, and he returns the type of ActionResult. A controller can have a lot of action.

So how does a URL be 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 is the urlroutingmodule to locate the URL to the controller. And for the URL to be routed to which controller to go, these we can be completely defined by ourselves. Let's go to the Global.asax file to see:

We can see here that a route named "Default" is defined, and the default parameters are defined. The meaning of the default parameter is that when we visit a URL such as http://localhost:2176/, he will add the default parameter to the non-existent parameter, which is equivalent to accessing Http://localhost:2176/Home/Index.

Note: We know that in IIS, when we visit the root directory of a Web site, if we do not specify the path to be accessed, IIS will access it itself according to the default document set in IIS. For example, when we visit the URL of http://localhost:2176/, IIS looks for the Default.aspx file under the root directory of the Web site (assuming we set the default document for IIS to Default.aspx). In ASP., the path to a Web site root directory like http://localhost:2176/is not processed by route, so we see the ASP. There is a default.aspx file in the root of the MVC program that is used to process the previous access to the root directory. Please do not delete the file. It will take http://localhost:2176/Default.aspx to ASP. NET MVC, 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 accessed as follows:

/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, if the file cannot be found, it will go to the Share directory to look for:/views/share/index.aspx, if not found, will throw the exception that cannot find the view. Return View ("lulu.aspx") to specify which view:/views/home/lulu.aspx to return.

So why is there a 404 error in the previous direct access to views/home/index.aspx file here, saying that the file could not be found? Because in MVC, it is not recommended to go directly to the view, so we built the ASP. NET MVC program by default in the Views directory with a Web. config file, the following:

That is, access to the views directory of all the files will be processed by System.Web.HttpNotFoundHandler, so please do not put resources (CSS, JS, pictures, etc.) in the views directory. If you really want to put in the views directory, please modify 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!

Introduction to ASP. NET MVC 1

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.