2012-07-22 69 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))); 

} 

这个工作对我们来说,当然来得把有关代码的区域在主项目的开销呢。