2012-11-19 34 views
26

Tôi đã tạo Ứng dụng MVC có 3 Vùng khác nhau. (Quản trị viên, Thành viên, News) Đây là RouteConfig.cs tập tin của tôi trong thư mục App_Start:Cách đăng ký các khu vực để định tuyến

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" } 
     ); 
    } 
} 

Và Đây là AdminAreaRegisteration.cs tập tin của tôi:

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" }     
      ); 
     } 
    } 
} 

Và cuối cùng này là Global.asax của tôi .cs nội dung tệp:

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(); 
     } 
    } 
} 

Trang chủ của trang web của tôi được tải đầy đủ và trang này hoạt động. nhưng Trang chủ của Quản trị viên hoặc các khu vực khác không được phát hiện theo tuyến đường và tôi đã đưa ra thông báo lỗi sau:

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 

Làm cách nào để giải quyết vấn đề này? Cảm ơn.

+1

Bạn có thực sự có bộ điều khiển nhà trong khu vực quản trị của mình không? – James

+0

Có, tôi có một lớp HomeController.cs cho từng Vùng. – Mojtaba

+0

Bộ điều khiển HomeController của bạn có phương pháp Chỉ mục không? Bạn đã ghi đè thuộc tính 'AreaName' chưa? – James

Trả lời

26

Gọi AreaRegistration.RegisterAllAreas() đâu đó trong bạn RegisterRoutes

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

Mẹo: Sử dụng một công cụ như RouteDebugger 2.0 hoặc Routing Debugger để điều tra các tuyến đường của bạn

Get mới nhất NuGet: Route Debugger for MVC hoặc RouteDebugger for WepApi

Dưới đây là một hướng dẫn trên How to set up and use RouteDebugger with WebApi

+1

Mã này nằm trong tệp Global.asax.cs của tôi và Visual Studio đã tạo các phương thức này. – Mojtaba

+1

Điều gì về Trình gỡ lỗi định tuyến? Bạn có thể đăng kết quả không? –

+0

Tôi đã sử dụng trình gỡ lỗi tuyến đường nhưng trang không thể tải được và vẫn còn sự cố. trình gỡ lỗi tuyến đường chỉ hiển thị các tuyến đường trong các trang đã tải. – Mojtaba

18

Từ mã được cung cấp tôi có thể thấy 2 vấn đề tiềm năng:

  1. You are not gọi RegisterAllAreas
  2. Bạn dường như không được trọng các AreaName tài sản

Hãy thử thay đổi của bạn mã tới:

Toàn cầu.asax

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    AreaRegistration.RegisterAllAreas(); 
    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     namespaces: new[] { "TestMvcApplication.Controllers" } 
    ); 
} 

khu vực quản lý

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 } 
     ); 
    } 
} 
4

Chỉ cần tạo một tên lớp tĩnh nó AreaConfig với một phương pháp tĩnh RegisterAreas() đây mã:

public static class AreaConfig 
{ 
    public static void RegisterAreas() 
    { 
     // 
     // Admin area . . . 

     var adminArea = new AdminAreaRegistration(); 
     var adminAreaContext = new AreaRegistrationContext(adminArea.AreaName, RouteTable.Routes); 
     adminArea.RegisterArea(adminAreaContext); 


     // 
     // Default area . . . 

     var defaultArea = new DefaultAreaRegistration(); 
     var defaultAreaContext = new AreaRegistrationContext(defaultArea.AreaName, RouteTable.Routes); 
     defaultArea.RegisterArea(defaultAreaContext); 
    } 
} 

sau đó gọi nó trong tệp Global.asax.cs như sau:

protected void Application_Start() 
    { 
     . . . 

     AreaConfig.RegisterAreas(); 

     . . . 
    } 
Các vấn đề liên quan