ASP. net mvc 5 getting started tutorial (3) routing route and mvcroute

Source: Internet
Author: User

ASP. net mvc 5 getting started tutorial (3) routing route and mvcroute

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

Download source code: Click here to download

The previous section describes how to create a controller. This section describes how to access the controller and its actions through routing.

So the question is, how can I write a url to access the Controller I want to access?

To access the vro correctly, you must know the routing rules. It is written in the RoutConfig. cs file in the App_Start folder of the solution. As the name suggests, this is the route configuration file. The Code is as follows.

using System.Web.Mvc;using System.Web.Routing;namespace SlarkInc{    public class RouteConfig    {        public static void RegisterRoutes(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 routing rules.

The first line indicates that the Action of the controller can be called through "http: // host name: Port Number/controller name/Action name/parameter id" and the Action carries the parameter id.

Let's first create a controller like this.

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

using System.Web.Mvc;namespace SlarkInc.Controllers{    public class FirstController : Controller    {        public string Index(string id)        {            return "This is first controller index page.<br/> Your Id is " + id;        }        public string Another()        {            return "This is first controller another page";        }    }}

The marked part of the code is the Action we want to access.

Press F5 to start the program.

We want to access the Index Action under the First controller and the parameter id.

Input formula: "http: // host name: Port Number/controller name/Action name/parameter id"

Http: // localhost: 57231/first/index/2

The result is as follows:

OK.

Next, let's take a look at the second line marked by the RoutConfig. cs file. As follows:

url: "{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

The second line means that if the controller is not written, the default controller is Home. If no action is written after determining the controller, the default action is Index. Id = UrlParameter. Optional indicates that the id can or cannot be. For example, when the Index Action of the First controller is called, there is a parameter, and the id is entered. If the Another Action of the First controller is called, no parameter is required.

The url is like this: http: // localhost: 57231/first/another

The running result is as follows:

Now I want the program to display the Index Action of FirstController as soon as it is started, so that testing is convenient. What should I do?

Set FirstController and Index Action to the default value.

url: "{controller}/{action}/{id}",defaults: new { controller = "First", action = "Index", id = UrlParameter.Optional }

The running result is as follows. Id is not given, so it is null, so there is no Id here:

Next, we can create a View. Coming soon.

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.