我創建了具有3個不同區域的MVC應用程序。 (管理員,用戶,新聞) 這是我RouteConfig.cs文件中App_Start目錄:如何註冊路由區域
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" }
);
}
}
,這是我AdminAreaRegisteration.cs文件:
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" }
);
}
}
}
最後,這是我的Global.asax .cs文件內容:
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();
}
}
}
我的網站主頁完全加載,它的工作原理。但管理員或其他地區的主頁不被檢測的路線,我給這個錯誤信息:
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
我怎樣才能解決這個問題? 謝謝。
實際上你的管理員區域是否有家庭控制器? – James
是的,我有一個每個區域的HomeController.cs類。 – Mojtaba
你的HomeController是否有索引方法?你是否重寫了'AreaName'屬性? – James