ASP. NET MVC and areas simple control routing

Source: Internet
Author: User

How to control the route in ASP. I want to close the article a lot, I am here is self-summary, for reference only.

1. We create a new project to view RouteConfig.cs, the code is as follows:

1  Public Static voidregisterroutes (routecollection routes)2         {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 5 routes. MapRoute (6Name"Default",7Url:"{Controller}/{action}/{id}",8DefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional}9             );Ten}

Line 3rd, routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Indicates that a route with the extension Axd is ignored.

You can follow it, and if you have any files in your project that are not externally accessible, you can filter them all out.

1 routes. MapRoute (2Name"Default",3Url:"{Controller}/{action}/{id}",4DefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional}5);

This is the most basic routing control. Indicates that the home page is the ActionResult Index in HomeController and the default route parameter is ID

Let's change this code, as follows:

1 ///Home Page2 routes. MapRoute (3                 "Index",//Route name4                 "{controller}/{action}",5                 New{controller ="Index", action ="Index", id =urlparameter.optional},6                 New string[] {"snscwan.controllers" }7);

There are notes that everyone should understand, where new string[] { "snscwan.controllers" } Snscwan is the name of the current site, this first note.

1 ///Paystepindex2 routes. MapRoute (3                 "Paystepindex",//Route name4                 "{controller}/{action}/{method}/{id}.html",5                 New{controller ="Paystep", action ="Index", Method = urlparameter.optional, id =urlparameter.optional},6                 New string[] {"snscwan.controllers" }7);

This is a route that controls multiple parameters, and you can change "{controller}/{action}/{method}/{id}.html" to set the route you need.

Below we create a new admin areas, representing the site's Administrator section, the default generated code is as follows:

1  Public Override voidRegisterarea (arearegistrationcontext context)2         {3 context. MapRoute (4                 "Admin_default",5                 "Admin/{controller}/{action}/{id}",6                 New{action ="Index", id =Urlparameter.optional}7             );8}

The same site, if the root directory under the route and areas route to a index/index at the same time, the project will be error, tell you that there are 2 simultaneous routes, this time how to deal with it? As follows:

1 context. MapRoute (2                 "Admin_default",3                 "Admin/{controller}/{action}/{id}",4                 New{controller ="Index", action ="Index", id =urlparameter.optional},5                 New string[] {"SnsCWan.Areas.Admin.Controllers" }6);

Special idea new string["{snscwan.areas.admin.controllers "}, the difference between this and the root directory, which represents the specified snscwan areas area admin Controller,

As a result, there is no more conflict between the routes.

Always configure the route is these, specifically how to set a more beautiful URL still need your more creative ideas.

This group provides ASP. NET Mvc,ef,linq,web API technical support, do not care about people, care about human spermatogonial.
ASP. NET MVC Group 171560784
Invite all the experts, beginners to join.

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.