ASP. NET MVC 5 Getting Started Tutorial (3) Routing route

Source: Internet
Author: User

Original: ASP. NET MVC 5 Getting Started Tutorial (3) Routing route

Article Source: slark.net-Blog Park http://www.cnblogs.com/slark/p/mvc-5-get-started-route.html

Previous section: ASP. NET MVC 5 Getting Started Tutorial (2) Controller controllers

Next section: ASP. NET MVC 5 Getting Started Tutorial (4) View and ViewBag

SOURCE download: Click I download

In the previous section we talked about how to create a controller, and this section discusses how to access the controller and its action by road.

So the question is, how can I write a URL to access the controller I want to access?

To access the router correctly, you know that the road is governed by the rules. It is written in the RoutConfig.cs file under the App_start folder of the solution. As the name implies, this is the routing configuration file. The code is as follows.

usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;namespaceslarkinc{ Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:  "{controller}/{action}/{id}"  ,defaults:                  New {controller = "Home", action = "Index" , id = urlparameter.optional} ); }    }}

The two lines marked in the code are the key to the routing rules.

The first line means that the action of this controller can be called by the "//hostname: Port number/Controller name/action/parameter ID" and the action has a parameter ID.

Let's start by creating a controller like this.

Open the FirstController.cs file under the Controllers folder under solution. Write the following code.

usingSYSTEM.WEB.MVC;namespaceslarkinc.controllers{ Public classFirstcontroller:controller {Public   string Index (string  ID) { return "This was firstcontroller index page.<br/> Your Id is " + ID; }         Public stringanother () {return "This is first controller another page"; }    }}

The markup part of the code is the action we want to access.

Press F5 to start the program.

We are going to access the index action under the first controller and the parameter is the ID.

Substituting formula: "//hostname: Port number/Controller name/action name/parameter ID"

Get HTTP://LOCALHOST:57231/FIRST/INDEX/2

The results are as follows:

Ok.

Let's look at the second line marked in the RoutConfig.cs file. As follows:

" {Controller}/{action}/{id} "  New"Home""Index", id = Urlparameter.optional}

The second line means that if the controller is not written, the controller will default to home. If you do not write the action after the controller is determined, the action defaults to index. ID = urlparameter.optional Indicates that the ID can or may not be. For example, when calling the first Controller's index action, there is an argument, and the ID is written at this time. If you call the first controller's another Action, no arguments are written.

So the URL is this: http://localhost:57231/first/another

The results of the operation are as follows:

Now I want the program to show Firstcontroller's index Action as soon as it starts up so it's easy to test. What to do?

Set the Firstcontroller and index action to the default.

" {Controller}/{action}/{id} "  New"First""Index", id = Urlparameter.optional}

The results of the operation are as follows. ID not given, so it is null, there is no ID:

Next we can start creating the view.

Next section: ASP. NET MVC 5 Getting Started Tutorial (4) View and ViewBag

ASP. NET MVC 5 Getting Started Tutorial (3) Routing route

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.