Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register areas for routing

I created MVC Application that have 3 different Area. (Admin, User, News) This is my RouteConfig.cs File in App_Start directory:

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 },             namespaces: new[] { "TestMvcApplication.Controllers" }         );     } } 

And This is my AdminAreaRegisteration.cs file:

    namespace TestMvcApplication.Areas.Admin {     public class AdminAreaRegistration : AreaRegistration     {         public override string AreaName         {             get             {                 return "Admin";             }         }          public override void RegisterArea(AreaRegistrationContext context)         {             context.MapRoute(                 "Admin_default",                 "Admin/{controller}/{action}/{id}",                 new { controller = "Home", action = "Index", id = UrlParameter.Optional },                 namespaces: new[] { "TestMvcApplication.Areas.Admin.Controllers" }                             );         }     } } 

And finally this is my Global.asax.cs file content:

namespace TestMvcApplication {     // Note: For instructions on enabling IIS6 or IIS7 classic mode,      // visit http://go.microsoft.com/?LinkId=9394801      public class MvcApplication : System.Web.HttpApplication     {         protected void Application_Start()         {             AreaRegistration.RegisterAllAreas();              WebApiConfig.Register(GlobalConfiguration.Configuration);             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);             RouteConfig.RegisterRoutes(RouteTable.Routes);             BundleConfig.RegisterBundles(BundleTable.Bundles);             AuthConfig.RegisterAuth();         }     } } 

Home page of my website fully loaded and it's works. but Homepage of Admin or other areas are not detect by route and I gave this error message:

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.  Requested URL: /Admin/Home 

How can I solve this problem? Thanks.

like image 989
Mojtaba Avatar asked Nov 19 '12 13:11

Mojtaba


People also ask

How can I register my area in MVC?

In the application startup code, you can call AreaRegistration. RegisterAllAreas() to register all areas. This needs a routing cs file for each area as described in the preceding article. Once registered, all the areas will start showing up in the Routing Table.

What is area routing?

The routing area, abbreviated as RA, is the counterpart of the location area (LA) in packet-switched (PA) networks. The RA is usually a smaller area compared to the LA because using multimedia services requires more frequent paging messages.

What are the three segments for routing?

The three segments of a default route contain the Controller, Action and Id.

What is the role of register route method?

The RegisterRoutes() route has a parameter that is a collection of routes called the RouteCollection that contains all the registered routes in the application. Figure 1.2 represents the default method that adds routes to the route table.


Video Answer


1 Answers

Call AreaRegistration.RegisterAllAreas() somewhere in your RegisterRoutes

public static void RegisterRoutes(RouteCollection routes) {     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");     AreaRegistration.RegisterAllAreas();     .... } 

Tip: Use a tool like RouteDebugger 2.0 or Routing Debugger to investigate your routes

Get latest NuGet: Route Debugger for MVC or RouteDebugger for WepApi

Here's a tutorial on How to set up and use RouteDebugger with WebApi

like image 54
Lukas Winzenried Avatar answered Sep 25 '22 00:09

Lukas Winzenried