2012-07-22 70 views
1

我有一個MVC 3應用程序與區域,並且我暴露了來自特定區域和控制器的服務。路由到該服務的AreaRegistration內像這樣定義ASP.NET MVC中的ServiceRoute與區域攔截動作鏈接到家

public class AreaAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get { return "Area"; } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.Routes.Add(
      new ServiceRoute("Area/Controller/Service", 
       new NinjectServiceHostFactory(), typeof(MyService))); 

     // .... 
    } 
} 

在我Global.asax.cs我只定義了默認路由

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Default", 
      "{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

在我_Layout.chshtml我有一個鏈接到我的主頁,在這裏我給一個空的區域,我希望它找到HomeController中的頂部的Controllers文件夾中的Index動作(位於Areas文件夾外部):

@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null) 

出於某種原因,這個ActionLink呈現爲

~/Area/Controller/Service?action=Index&controller=Home 

如果我註釋掉ServiceRoute,同樣ActionLink~/這是我的期望。

任何想法如何解決這個路由問題?我發現的唯一解決方法是使用此代替:

<a href="@Url.Content("~/")">Home</a> 

回答

0

我們有這個完全相同的問題。路由註冊的順序似乎是問題,因爲來自區域的路由將在來自global.asax代碼的路由之前註冊。

要解決此問題,允許URL路由到服務以及防止回髮針對服務URL,請嘗試在註冊其他路由後將ServiceRoute添加到Global.asax.cs中。

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     "Default", 
     "{controller}/{action}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 

    context.Routes.Add(
     new ServiceRoute("Area/Controller/Service", 
      new NinjectServiceHostFactory(), typeof(MyService))); 

} 

這個工作對我們來說,當然來得把有關代碼的區域在主項目的開銷呢。