2015-11-28 105 views
0

我有從區域視圖後向鏈接到非區域視圖的問題。從區域視圖非區域視圖鏈接ASP MVC

當前結構

Web應用程序樹:

  • /控制器/ BaseController.cs
  • /瀏覽/基/ Index.cshtml
  • /地區/ Area1/Controller/設置Controller.cs
  • /地區/區域1 /瀏覽/設置/ 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 } 
     ); 
    } 
} 

註冊路線CONFIGS(Global.asax.cs中)

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    [..] 

問題

當我基本視圖內(/查看/基材/ Index.cshtml)代碼@Html.ActionLink("My home link", "Index", "Home")生成我預計的鏈接http://localhost:81/de-DE/Home

當我(設置/ Index.cshtml /地區/區域1 /瀏覽/)相同的代碼生成鏈路http://localhost:81/de-DE/Area1/Home但這指向無處一個區域視圖中。

試過到目前爲止

我瞭解到,代碼@Html.ActionLink("My home link", "Index", "Home", new { area = ""}, null)作品都,區域和非區域的觀點,導致正確的http://localhost:81/de-DE/Home視圖。

問題

我怎樣才能structur我的路線CONFIGS的方式,調用鏈接創建梅索德沒有面積參數始終鏈接到基礎視圖/控制器?

還是有更好的解決方案來實現這一目標嗎?

我想到的是:

@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/面積/行動

回答

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); 
    } 
    } 
} 

正如你可以看到,如果你不通過的區中的值,它會採取目前的區域,您在

我能想到的唯一解決方案就是創建自己的HTML擴展。類似的東西:

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