【问题标题】:ASP MVC linking from area view non area view从区域视图非区域视图的 ASP MVC 链接
【发布时间】:2015-11-28 00:29:26
【问题描述】:

我在从区域视图反向链接到非区域视图时遇到问题。

当前结构

Web 应用程序树:

  • /Controller/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") 会生成我期望的链接 http://localhost:81/de-DE/Home

当我在区域视图 (/Areas/Area1/Views/Setting/Index.cshtml) 中时,相同的代码会生成一个链接 http://localhost:81/de-DE/Area1/Home,但这指向无处。

目前尝试过

我了解到代码 @Html.ActionLink("My home link", "Index", "Home", new { area = ""}, null) 在区域视图和非区域视图中都有效,并导致正确的 http://localhost:81/de-DE/Home 视图。

问题

我怎样才能以调用没有区域作为参数的链接创建方法始终链接到基本视图/控制器的方式来构建我的路由配置?

或者有更好的解决方案来实现这一点?

我的期望是:

@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/区域/动作

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    这与路由无关。这是 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);
        }
      }
    }
    

    如您所见,如果您不传递 area 值,它将采用您所在的当前区域。

    我能想到的唯一解决方案是创建自己的 HTML 扩展。类似的东西:

    public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
    {
       return htmlHelper.ActionLink(linkText, actionName, controllerName, new { area = String.Empty });
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 2011-04-02
      相关资源
      最近更新 更多