Analysis of ASP. net mvc application

Source: Internet
Author: User

To fully understand how Asp.net MVC works, I will create an MVC application from scratch.Program.

1. Create a new ASP. NET web application. It includesDefault. aspxPage, a standardWeb. configFile and add some initial references.

2. Add theSystem. Web. Export actions. dll","System. Web. Routing. dllAndSystem. Web. MVC. dllAll of these can be referenced in "C: \ Program Files \ Microsoft ASP. net \ ASP.. Net MVC beta \ assemblies folder ).

Use mvchttphandler to process MVC requests. Open the code-behind file (default. aspx. CS) of default. aspx ). The page_load method processes requests in MVC mode.

 
Protected VoidPage_load (ObjectSender,EventargsE)
 
{
 
Httpcontext. Current. rewritepath (request. applicationpath );
IhttphandlerHttphandler =New Mvchttphandler();
 
Httphandler. processrequest (Httpcontext. Current );
 
}
 
 

3. Add a global application class (Global. asax ). In the application_start method, map the route to the home controller.

 
Protected VoidApplication_start (ObjectSender,EventargsE)
 
{
 
Routetable. Routes. maproute ("Default route",
 
"{Controller}/{action }",
New{Controller ="Default", Action ="Index"});
 
}
 
 

4. To use the maproute and ignoreroute methods, you must first use the using system. Web. MVC namespace (because they are extension methods ). The maproute method uses the route name as the first parameter, uses the URI template as the second parameter (for example, "{controller}/{action}"), and uses the default value as the third parameter. It should be noted that the default object should have attributes to match the attributes in the URI template.CodeThe default object has two attributes: controller and action, which are used to match {controller} and {action} in the URI template }). The preceding route maps a URL to contoller and action.

5. Create a default controller. In the web application's controllers folder, create a class named defacontroller controller. Note the naming conventions here. default is the default value of route, while "controller" is only a suffix specified in the naming conventions.

The Controller class should inherit from system. Web. MVC. Controller and include a public method as action. Because the default action is index (as can be seen from the default route), this class should look like this:

Public Class Defacontroller Controller:Controller
 
{
 
Public StringIndex ()
 
{
 
Return "Hello, world";
 
}
 
}
 
 

6. Run the application and navigate to the application directory ("/"). You can see that the response is "Hello, world ".

 

However, if you try to navigate to the index action (/default/index) of the default controller, an error message is returned.

7. Add the URL routing module. Open Web. config, go to

<Httpmodules>
 
...
<Add Name="Urlroutingmodule"
 
Type="System. Web. Routing. urlroutingmodule, system. Web. Routing,
Version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
 
</Httpmodules>
 
 

8. Run the application and navigate to the index action of the default controller. Now you can see that the response results are the same as the previous ones.

 

9. Use a view as the return value of index action. Change the return value of index action of default controller to actionresult. Multiple types of results (such as jsonresult and contentresult) can be returned ). In this example, a viewresult is returned.

 
Public ActionresultIndex ()
 
{
 
ReturnView ();
 
}

Create the view page corresponding to this action. When you call a view () method without parameters, it searches for the view page with the same name as the action method in a folder with the same name as the controller. Create a new page: Index. aspx in the Views \ Default \ folder.

To make the page a view of MVC, open the code behind file (index. aspx. CS) and let the class inherit from system. Web. MVC. viewpage.

Modify the page (in design mode or source mode) to add greeting information:

 
<Body>
<Form ID="Form1" Runat="Server">
 
<Div>
 
<H1>Hello, world</H1>
</Div>
 
</Form>
 
</Body>
 
 

10. Run the application and you should be able to receive the Response Message from the view page we just created. The routing engine calls the index action of the default controller and returns the view page (index. aspx ).

11. display the data on the view page. Open Controller, find the index method, and add the data to the viewdata dictionary.

 
Public ActionresultIndex ()
 
{
 
Viewdata ["Name"] ="Guy";
ReturnView ();
 
}
 
 

Now, on the view page, use the data in the greeting line.

 
<Body>
 
<Form ID="Form1" Runat="Server">
 
<Div>
<H1>Hello,<% = Viewdata ["Name"] %></H1>
 
</Div>
 
</Form>
 
</Body>
 
 
 

12. Run the application and you can see the data added to the Controller.

 

In this articleArticleI have created an Asp.net MVC application from scratch to analyze Asp.net MVC and understand the magic behind the framework. This helps me to use MVC in the existing web application.

Original Author:Guy Burstein
Original article address: Anatomy of an ASP. net mvc application
Translation: inrie (Hong Xiaojun)

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.