ASP. net mvc and Areas simple control routing, mvcareas

Source: Internet
Author: User

ASP. net mvc and Areas simple control routing, mvcareas

There are many articles about how to control routing in ASP. net mvc. I will summarize them here for your reference only.

1. Create a new project and view RouteConfig. cs. The Code is as follows:

 1 public static void RegisterRoutes(RouteCollection routes) 2         { 3             routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 4  5             routes.MapRoute( 6                 name: "Default", 7                 url: "{controller}/{action}/{id}", 8                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 9             );10         }

Line 3: routes. IgnoreRoute ("{resource}. axd/{* pathInfo}"); indicates that routes with the axd extension are ignored,

You can follow this pattern. If any files in your project do not attack external access, you can filter them out.

1 routes.MapRoute(2                 name: "Default",3                 url: "{controller}/{action}/{id}",4                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }5             );

This is the most basic route control. The homepage is the ActionResult Index in HomeController. The default route parameter is id.

Next we will modify this code string, as shown below:

1 // homepage 2 routes. mapRoute (3 "Index", // Route name4 "{controller}/{action}", 5 new {controller = "Index", action = "Index", id = UrlParameter. optional}, 6 new string [] {"SnsCWan. controllers "} 7 );

You can understand the remarks. The SnsCWan in new string [] {"SnsCWan. Controllers"} is the name of the current website. Please pay attention to it first.

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 with multiple parameters under control. You can change "{controller}/{action}/{Method}/{id1_.html" to set the routing mode you need.

 

Next, we create an Admin Areas, which indicates the site administrator. The default generated code is as follows:

 

1 public override void RegisterArea(AreaRegistrationContext context)2         {3             context.MapRoute(4                 "Admin_default",5                 "Admin/{controller}/{action}/{id}",6                 new { action = "Index", id = UrlParameter.Optional }7             );8         }

 

In the same website, if both the routes under the root directory and the routes under Areas are routed to an Index/Index at the same time, an error is reported in the project, indicating that two routes exist at the same time, how can this problem be solved? 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 "}, which is different from the routing in the root directory. It indicates the Admin controller of the Areas region under the SnsCWan website,

In this way, there will be no conflict between routes.

You have to configure routes. You still need to be more creative in setting beautiful URLs.

 

This group provides technical support for ASP. net mvc, EF, LINQ, and WEB APIs. It does not care about many people, but about human essence.
ASP. net mvc group 171560784
We invite all experts and beginners to join us.

 


How does aspnet mvc set route startup?

You can make the following settings in global.
Public class MvcApplication: System. Web. HttpApplication
{
Public static void RegisterRoutes (RouteCollection routes)
{
Routes. IgnoreRoute (". axd /");
Routes. MapRoute (
"Xiaohong", // Route name
"Home/xiaohong", // URL with parameters
New // Parameter defaults
);

Routes. MapRoute (
"Chenghong", // Route name
"Home/chenghong", // URL with parameters
New // Parameter defaults
);

Routes. MapRoute (// note that Default must be written to the end
"Default", // Route name
"//", // URL with parameters
New // Parameter defaults
);

}

Protected void Application_Start ()
{

AreaRegistration. RegisterAllAreas ();

RegisterRoutes (RouteTable. Routes );
}
}
In addition, group purchases on Virtual Machine groups are extremely cheap.

Aspnet mvc url route ing

You can make the following settings in global.
Public class MvcApplication: System. Web. HttpApplication
{
Public static void RegisterRoutes (RouteCollection routes)
{
Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");
Routes. MapRoute (
"Xiaohong", // Route name
"Home/xiaohong", // URL with parameters
New {controller = "Home", action = "Index"} // Parameter defaults
);

Routes. MapRoute (
"Chenghong", // Route name
"Home/chenghong", // URL with parameters
New {controller = "Home", action = "Index"} // Parameter defaults
);

Routes. MapRoute (// note that Default must be written to the end
"Default", // Route name
"{Controller}/{action}/{id}", // URL with parameters
New {controller = "Home", action = "Index", id = UrlParameter. Optional} // Parameter defaults
);

}

Protected void Application_Start ()
{

AreaRegistration. RegisterAllAreas ();

RegisterRoutes (RouteTable. Routes );
... The remaining full text>

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.