[Go] Anatomy ASP. NET MVC Application

Source: Internet
Author: User
Tags naming convention

Http://www.cnblogs.com/errorif/archive/2009/02/13/1389927.html

To get a complete picture of how ASP. NET MVC works, I'll create an MVC application from scratch.

1. Create a new ASP. NET WEB application. It consists of having a Default.aspx page, a standard Web. config file, and adding some initial references.

2. Add references to "System.Web.Abstractions.dll", "System.Web.Routing.dll", and "System.Web.Mvc.dll" , all of which can be found in the C:\Program Files\Microsoft Asp.net\asp.net Mvc Beta\assemblies folder (the installation path for ASP. NET MVC).

Use Mvchttphandler to handle MVC requests. Open Default.aspx Code-behind file (Default.aspx.cs). In the Page_Load method, the request is processed in an MVC manner.

protected void Page_Load (object sender, EventArgs e)
{
HttpContext.Current.RewritePath (Request.applicationpath);
IHttpHandler HttpHandler = 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 void Application_Start (object sender, EventArgs e)
{
RouteTable.Routes.MapRoute ("Default Route",
"{controller}/{action}",
New {controller = "Default", action= "Index"});
}

4. In order to use the Maproute and Ignoreroute methods, you must precede the using SYSTEM.WEB.MVC namespace (because they are extension methods). The Maproute method takes the name of the route as the first parameter, with the URI template as the second parameter (such as "{controller}/{action}"), with the default value as the third parameter. It should be noted that the default value object should have properties that match the attributes in the URI template (as in the preceding code, the default object has two properties: Controller and action, which are used to match the {Controller} and {action} in the URI template). The route above maps a URL to contoller and action.

5. Create a default controller. Create a class in the Controllers folder of the Web application named Defaultcontroller. Notice the naming convention here, the default is the route defaults, and "Controller" is simply a suffix named in the naming convention.

The Controller class should inherit from System.Web.Mvc.Controller and should include a public method as the action. Because the default action is index (as you can see from the default route), the class should look like this:

public class Defaultcontroller:controller
{
{
Return "Hello, World";
}
}

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

However, if you try to navigate to the Default controller's Index Action (/default/index), you will get an error message.

7. Add URL Routing module. Open Web. config, navigate to <system.web> under


       
version=3.5.0.0, culture=neutral, publickeytoken=31bf3856ad364e35 "/>

8. Run the application and navigate to the default controller's index Action. Now you should be able to see that the response is the same as before.

9. Use a view as the return value of the index action. Change the index action of the default controller to the return value of ActionResult. There are many types of result that can be returned (such as Jsonresult, Contentresult, and so on). In this example, we will return a viewresult.

Public ActionResult Index ()
{
return View ();
}

Create the view page for the action. When a non-parametric view () method is called, it looks for a view page with the same name as the action method in a folder with the same name as the controller. Now create a new page under the Views\default\ folder: Index.aspx.

To make the page an MVC view, open the code behind file (Index.aspx.cs) and let the class inherit from System.Web.Mvc.ViewPage.

To modify the page (in design mode or source mode), add the greeting message:

<body>
<form id= "Form1" runat= "Server" >
<div>
</div>
</form>
</body>

10. Run the application and you should be able to receive the response from the view page we just created. The routing engine calls the default controller's Index Action and returns to the View page (index.aspx).

11. Displays the data for the View page. Open the controller, locate the index method, and add the data to the ViewData dictionary.

Public ActionResult Index ()
{
viewdata["name"] = "guy";
return View ();
}

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

<body>
<form id= "Form1" runat= "Server" >
<div>
</div>
</form>
</body>

12. Run the application to see the data added to the controller.

In this article, I created an ASP. NET MVC application from scratch to dissect ASP. And understand the magic behind the framework. This helps me to use MVC in my existing Web application.

Former Guy Burstein
Original address: Anatomy of an ASP. NETMVC application
Translation:Inrie (Hong Xiaojun)

[Go] Anatomy ASP. NET MVC Application

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.