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.
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
Có, tôi có một lớp HomeController.cs cho từng Vùng. – Mojtaba
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