ASP 从区域视图非区域视图链接的 MVC
ASP MVC linking from area view non area view
我在从区域视图返回到非区域视图时遇到问题link。
当前结构
Web 应用程序树:
- /控制器/BaseController.cs
- /Views/Base/Index.cshtml
- /Areas/Area1/Controller/SettingsController.cs
- /Areas/Area1/Views/Setting/Index.cshtml
默认路由配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Localization",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "de-DE", area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
区域路由配置:
public class Area1AreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Area1";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Photovoltaics_localized",
"{culture}/Photovoltaics/{controller}/{action}/{id}",
new { culture = "de-DE", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
注册路由配置(Global.asax.cs)
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
[..]
问题
当我在基本视图中时 (/Views/Base/Index.cshtml),代码 @Html.ActionLink("My home link", "Index", "Home")
生成 link我预计 http://localhost:81/de-DE/Home .
当我在区域视图中时 (/Areas/Area1/Views/Setting/Index.cshtml),相同的代码会生成 link http://localhost:81/de-DE/Area1/Home 但这指向无处。
目前已尝试
我了解到代码 @Html.ActionLink("My home link", "Index", "Home", new { area = ""}, null)
在区域视图和非区域视图中都有效,并导致正确的 http://localhost:81/de-DE/Home 视图。
问题
我如何构建我的路由配置,以调用一个 link 创建方法,没有区域作为参数总是 link 到基础 views/controllers?
或者有更好的解决方案吗?
我期望的是:
@Html.ActionLink("My home link", *action*, "controller")
= http://localhost:81/de-DE/动作
@Html.ActionLink("My home link", *action*, *controller*, new { area = *area*}, null)
= http://localhost:81/de-DE/面积/动作
这与路由无关。这是创建 ActionLink 方法 URL 的默认行为。您可以在以下代码中看到这一点(取自 ASP.NET MVC 代码集):
if (values != null)
{
object targetAreaRawValue;
if (values.TryGetValue("area", out targetAreaRawValue))
{
targetArea = targetAreaRawValue as string;
}
else
{
// set target area to current area
if (requestContext != null)
{
targetArea = AreaHelpers.GetAreaName(requestContext.RouteData);
}
}
}
如您所见,如果您不传递区域值,它将采用您所在的当前区域。
我能想到的唯一解决方案是创建您自己的 HTML 扩展。类似于此:
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, new { area = String.Empty });
}
我在从区域视图返回到非区域视图时遇到问题link。
当前结构
Web 应用程序树:
- /控制器/BaseController.cs
- /Views/Base/Index.cshtml
- /Areas/Area1/Controller/SettingsController.cs
- /Areas/Area1/Views/Setting/Index.cshtml
默认路由配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Localization",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = "de-DE", area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
区域路由配置:
public class Area1AreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Area1";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Photovoltaics_localized",
"{culture}/Photovoltaics/{controller}/{action}/{id}",
new { culture = "de-DE", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
注册路由配置(Global.asax.cs)
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
[..]
问题
当我在基本视图中时 (/Views/Base/Index.cshtml),代码 @Html.ActionLink("My home link", "Index", "Home")
生成 link我预计 http://localhost:81/de-DE/Home .
当我在区域视图中时 (/Areas/Area1/Views/Setting/Index.cshtml),相同的代码会生成 link http://localhost:81/de-DE/Area1/Home 但这指向无处。
目前已尝试
我了解到代码 @Html.ActionLink("My home link", "Index", "Home", new { area = ""}, null)
在区域视图和非区域视图中都有效,并导致正确的 http://localhost:81/de-DE/Home 视图。
问题
我如何构建我的路由配置,以调用一个 link 创建方法,没有区域作为参数总是 link 到基础 views/controllers?
或者有更好的解决方案吗?
我期望的是:
@Html.ActionLink("My home link", *action*, "controller")
= http://localhost:81/de-DE/动作
@Html.ActionLink("My home link", *action*, *controller*, new { area = *area*}, null)
= http://localhost:81/de-DE/面积/动作
这与路由无关。这是创建 ActionLink 方法 URL 的默认行为。您可以在以下代码中看到这一点(取自 ASP.NET MVC 代码集):
if (values != null)
{
object targetAreaRawValue;
if (values.TryGetValue("area", out targetAreaRawValue))
{
targetArea = targetAreaRawValue as string;
}
else
{
// set target area to current area
if (requestContext != null)
{
targetArea = AreaHelpers.GetAreaName(requestContext.RouteData);
}
}
}
如您所见,如果您不传递区域值,它将采用您所在的当前区域。
我能想到的唯一解决方案是创建您自己的 HTML 扩展。类似于此:
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, new { area = String.Empty });
}