如何在ASP.NET MVC中将“区域"视图设置为主页? [英] How to set Area view as home page in ASP.NET MVC?
问题描述
如何在包含Areas的ASP.NET MVC应用程序中将视图设置为域的主页的路由.我需要一个特定区域的视图才能成为主页.该怎么办?
How to setup route for a view to be as home page of a domain in ASP.NET MVC application which contains Areas. I need a view of a particular area to be home page. How could this be done?
我尝试使用以下代码未成功.
I tried using the following code without any success.
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index" },
namespaces: new string[] { "WebApp.Areas.UI.Controllers" }
);
}
推荐答案
在Area文件夹中,有一个名为 AreaName AreaRegistration的文件,该文件源自AreaRegistration,它具有一个RegisterArea函数,用于设置路由
In the Area folder there is a file by name AreaNameAreaRegistration deriving from AreaRegistration, it has a function RegisterArea which sets up the route.
区域中的默认路由为 AreaName /{controller}/{action}/{id}.对此进行修改可以将一个区域设置为默认区域.例如,我根据需要将默认路由设置为{controller}/{action}.
Default route in an Area is AreaName/{controller}/{action}/{id}. Modifying this can set an area as default area. For example I set the default route as {controller}/{action} for my requirement.
public class UIAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "UI";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UI_default",
"{controller}/{action}/{id}", //******
new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
}
这篇关于如何在ASP.NET MVC中将“区域"视图设置为主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!