[ASP. NET MVC] ASP. MVC5 Series--First project

Source: Internet
Author: User

Directory

Overview

Create the first project

Add Controller

Summarize

Overview

This tutorial is an individual step by step learning summary, hoping to help the MVC5 is entering the direction of the friend, the individual is also ready to enter the field of ASP. MVC5, although difficult, but not the music. Who makes us like programming? Previously contacted with the ASP. NET MVC4, today to see the difference is still there, whether the use of the IDE to create the way, or use the way some places are really different. The hand also does not have the ASP. NET MVC5 the tutorial, can only look at the English website, step by step grope. In fact, I have always wanted to use MVC, can backfire ah, to the present still use WebForm.

Create the first project

Using ide:vs2013

To create a project, let's look at what the legendary ASP. NET MVC looks like. Other knowledge is interspersed in the project.

Create an MVC project, you can choose MVC or empty, choose MVC will create a piece of things including bootstrap front-end frame, I am a classic machine, very card, so I chose empty, create a bit faster. No way ah, even if the old machine also have to study ah.

Select empty to create the project structure as follows:

Project Brief Introduction

RouteConfig.cs: The Routing Rule Configuration class (remember that the previous version appears to be in the Global.asax file).

1      Public classRouteconfig2     {3         /// <summary>4         ///methods for registering routes5         /// </summary>6         /// <param name= "routes" >The route collection, since the collection is definitely able to add more than one routing rule, is described later in this section. </param>7          Public Static voidregisterroutes (routecollection routes)8         {9             //ignores the specified URL route for a given list of available routesTenRoutes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); One             //route map, mapping specified URL routes and setting default route values A routes. MapRoute ( -Name"Default",//The name of the route to map.  -Url:"{Controller}/{action}/{id}",//the URL pattern for the route. {Controller}/{operation}/{parameter} theDefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional} -             ); -         } -}

So what has Global.cs become now?

1      Public classMvcApplication:System.Web.HttpApplication2     {3         /// <summary>4         ///Application Start Method5         /// </summary>6         protected voidApplication_Start ()7         {8             //registering all areas in the application9 Arearegistration.registerallareas ();Ten             //Registering Routes One routeconfig.registerroutes (routetable.routes); A         } -}

Basic knowledge

MVC is all called Model-view-controller (model-View-controller). MVC is a model for developing applications that already have a well-architected framework and are very easy to maintain. Applications developed using MVC typically include the following pieces of content:
• Controller: The Controller class handles requests made by the client to the Web application, obtains data, and specifies a view that is returned to the client to display the results of the processing.
• Model: The model class represents the data of the application, which typically has a data validation logic that is used to make the data conform to the business logic.
• View: A view class is a template file in a Web application that generates and displays the results of a server-side response to a client request in HTML format.

Add Controller

Add a Controller

is not frightened, so many ways, indeed very shocking. Start with the empty controller, then use the other to introduce it slowly.

Note Adding the controller name must be noted that the controller name must end with the controllers, to ask why, feel the same as the attribute features, rules! Beginner entry level does not discuss this. Add a Indexcontroller.

Take a look at how the project structure changes.

Yes, as shown in the red box, adding a controller, by default, creates a name in the view (if so named Controller-name +controller) the same folder.

Enter IndexController.cs

1      Public classIndexcontroller:controller2     {3         //4         //GET:/index/5          Publicactionresult Index ()6         {7             returnView ();8         }9}

Let's change the code in Indexcontroller as follows:

1      Public classIndexcontroller:controller2     {3         //4         //GET:/index/5          Public stringIndex ()6         {7             return "This is my <b>default</b> action ...";8         }9         // Ten         //GET:/helloworld/welcome/ One  A          Public stringWelcome () -         { -             return "This is the Welcome action method ..."; the         }  -}

In the example, the Controller method returns an HTML string. Let's access the first method of the controller Indexcontroller in the browser index () and press F5 to run it directly.

http://localhost:4585, and then spell the/index,http://localhost:4585/index after the address, the page in the browser looks just like the one shown. In the above method, the code returns a string directly.

ASP. NET MVC calls different controller classes (or different methods) based on the URL of the input. The ASP. NET MVC default URL routing logic uses the following format to determine what code is called:

/[controller]/[actionname]/[parameters]

You can set the file format in the App_start/routeconfig.cs routing settings.

1      Public classRouteconfig2     {3         /// <summary>4         ///methods for registering routes5         /// </summary>6         /// <param name= "routes" >The route collection, since the collection is definitely able to add more than one routing rule, is described later in this section. </param>7          Public Static voidregisterroutes (routecollection routes)8         {9             //ignores the specified URL route for a given list of available routesTenRoutes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); One             //route map, mapping specified URL routes and setting default route values A routes. MapRoute ( -Name"Default",//The name of the route to map.  -Url:"{Controller}/{action}/{id}",//the URL pattern for the route. {Controller}/{operation}/{parameter} theDefaultsNew{controller ="Index", action ="Index", id =Urlparameter.optional} -             ); -         } -}

When you run the application, the "index" method in the "index" controller is executed by default. If you access the Welcome method under the index controller, you can modify the

1      Public classRouteconfig2     {3         /// <summary>4         ///methods for registering routes5         /// </summary>6         /// <param name= "routes" >The route collection, since the collection is definitely able to add more than one routing rule, is described later in this section. </param>7          Public Static voidregisterroutes (routecollection routes)8         {9             //ignores the specified URL route for a given list of available routesTenRoutes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); One             //route map, mapping specified URL routes and setting default route values A routes. MapRoute ( -Name"Default",//The name of the route to map.  -Url:"{Controller}/{action}/{id}",//the URL pattern for the route. {Controller}/{operation}/{parameter} theDefaultsNew{controller ="Index", action ="Welcome", id =Urlparameter.optional} -             ); -         } -}

If you run the application directly by pressing F5, you will get the results shown:

Now, let's modify the welcome method to give him two parameters, string and int type parameters.

1          Public stringWelcome (stringName ="Wolfy",intAge = at)2         {3             returnHttputility.htmlencode ("Hello"+ name +"that you are not this year"+ Age. ToString () +"?");4}

Returns an HTML-encoded return value for the preceding code. Then F5 run.

So now the value of the parameter is modified by the URL, url:http://localhost:4585/index/welcome?name=zhangsan&age=34

Is there a way to get the URL parameters in the format we want to enter? There are rules that you can add by adding a routing rule:

If the parameter enters Http://localhost:4585/Index/Welcome/lisi/30 in this format,

Summarize

This article describes how to return HTML directly using the methods in the controller, which is usually not what we want. Instead, we typically use a separate view template file to generate an HTML response. The use of routing is also briefly introduced in the article insertion. At present, do not tangle these things, first know how to use, then will be in-depth introduction of each knowledge point.

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.