2012-02-04 23 views
5

Trong ứng dụng ASP.NET MVC 3 có các khu vực (xem lược đồ bên dưới). Thư mục "Controllers" từ thư mục gốc đã bị xóa.Cùng một tên người dùng trong các khu vực khác nhau

Khi tôi làm điều này:

routes.MapRoute(
     "Default", // Route name 
     "{controller}/{action}/{id}", // URL with parameters 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
    ); 

tôi nhận được lỗi này: Nhiều loại đã được tìm thấy phù hợp với bộ điều khiển có tên là 'Trang chủ'. Điều này có thể xảy ra nếu tuyến đường dịch vụ yêu cầu này ('{controller}/{action}/{id}') không chỉ định không gian tên để tìm kiếm bộ điều khiển phù hợp với yêu cầu. Nếu trường hợp này xảy ra, hãy đăng ký tuyến đường này bằng cách gọi quá tải phương thức 'MapRoute' có tham số 'không gian tên'. Yêu cầu 'Trang chủ' đã tìm thấy các bộ điều khiển phù hợp sau đây: MyProject.Areas.Administration.Controllers.HomeController MyProject.Areas.BackEnd.Controllers.HomeController MyProject.Areas.FrontEnd.Controllers.HomeController

Khi tôi làm điều này:

routes.MapRoute(
     "Default", // Route name 
     "{controller}/{action}/{id}", // URL with parameters 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
     new string[] { "MyProject.Areas.Administration.Controllers" } 
    ); 

tôi nhận được tis lỗi:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: 
~/Views/Home/Index.aspx 
~/Views/Home/Index.ascx 
~/Views/Shared/Index.aspx 
~/Views/Shared/Index.ascx 
~/Views/Home/Index.cshtml 
~/Views/Home/Index.vbhtml 
~/Views/Shared/Index.cshtml 
~/Views/Shared/Index.vbhtml 


---- Areas 
     | 
     |---- Administration 
     |  |--- Controllers 
     |  |  |---- HomeController 
     |  |--- Views 
     |    |--- Index 
     |---- FrontEnd 
     |  |--- Controllers 
     |  |  |---- HomeController 
     |  |--- Views 
     |    |--- Index 
     |---- BackEnd 
       |--- Controllers 
       |  |---- HomeController 
       |--- Views 
         |--- Index 

Update1 Để bắt đầu một bộ điều khiển cụ thể trong khu vực, tôi đã cố gắng này:

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

    routes.MapRoute(
     "Default", 
     "{controller}/{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     new[] { "MyProject.Areas.BackEnd.Controllers" } 
    ); 
} 

Trả lời

11

Hãy thử như sau:

~/Global.asax.cs:

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

    routes.MapRoute(
     "Default", 
     "{controller}/{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     new[] { "MyProject.Controllers" } 
    ); 
} 

~/Areas/Administration/AdministrationAreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "Administration_default", 
     "Administration/{controller}/{action}/{id}", 
     new { action = "Index", id = UrlParameter.Optional }, 
     new[] { "MyProject.Areas.Administration.Controllers" } 
    ); 
} 

~/Areas/FrontEnd/FrontEndAreaRegistration.cs:

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "FrontEnd_default", 
     "FrontEnd/{controller}/{action}/{id}", 
     new { action = "Index", id = UrlParameter.Optional }, 
     new[] { "MyProject.Areas.FrontEnd.Controllers" } 
    ); 
} 

Bây giờ khi bạn yêu cầu /Administration/Home/Index, các Index hành động của HomeController trong khu vực Administration sẽ được gọi và nó sẽ tìm kiếm quan điểm ~/Areas/Administration/Views/Home/Index.cshtml. Đảm bảo chế độ xem này có tại vị trí này. Trong hình của bạn, bạn dường như đã bỏ qua thư mục Home - ~/Areas/Administration/Views/Index.cshtml.

+0

Có vẻ ổn. Một suy nghĩ nữa. Khi tôi nhấn F5 trong VS, tôi muốn theo mặc định sẽ đến khu vực phụ trợ trong Home/Index –

+0

@ Kris-I, trong trường hợp này hoặc sửa đổi đăng ký tuyến đường khu vực của bạn hoặc đi đến các thuộc tính của dự án của bạn và trong tab Web bạn có thể thiết lập một trang khởi động mà bạn có thể định nghĩa 'Quản trị/Trang chủ/Chỉ mục'. Bằng cách này khi bạn chạy ứng dụng, nó sẽ tự động điều hướng đến bộ điều khiển này. –

+0

Thay đổi trang khởi động ok, nhưng bằng cách đăng ký tuyến đường hãy xem cập nhật1. Cảm ơn –

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