2016-06-07 72 views
3

我有一个MVC 4应用程序。在那个应用程序中,我有某些区域,其URL不能正确解析。该LocationAreaRegistration.cs如下:设置路径到一个区域,但没有在MVC中的url中指定区域名称

context.MapRoute(
      "Location_default", 
      "{culture}/{controller}/{action}/{id}", 
      new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional }, 
      new { controller = "(LocationIndex)" } 
     ); 

route.config如下:

routes.MapRoute(
       name: "Default", 
       url: "{culture}/{controller}/{action}/{id}", 
       defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }, 
       namespaces: new[] { "Locator.Areas.Location.Controllers" } 
       ).DataTokens.Add("area", "Location"); 

我也试图改变route.config如下:

routes.MapRoute(
      name: "Default", 
      url: "{culture}/{controller}/{action}/{id}", 
      defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new[] { "Locator.Areas.Location.Controllers" } 
      ); 

的方法都没有工作,我收到The resource cannot be found错误。

然而,当我改变LocationAreaRegistration.cs如下,它的工作原理:

context.MapRoute(
      "Location_default", 
      "{culture}/Location/{controller}/{action}/{id}", 
      new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional }, 
      new { controller = "(LocationIndex)" } 
     ); 

但我不想要的网址包含了Location(地区名)。我究竟做错了什么?

编辑

,我会去会有点像网址:

http://localhost/en/LocationIndex/LocationIndex 

这里en是当前的文化,Homecontroller名称和Indexaction method名。

+0

好的一组MapRoutes,但不知道你要去的URL,这个问题是无法回答的。 –

+0

@ErikPhilips:我编辑了我的问题。请检查 –

+0

您的路由配置错误。请参阅[为什么要在asp.net mvc中的常用路由之前先映射特殊路由](http://stackoverflow.com/questions/35661062/why-map-special-routes-first-before-common-routes-in-asp-net -mvc/35674633)。 – NightOwl888

回答

3

为了使Location面积为你的MVC应用程序的路由的默认设置,你只需要定义RouteConfig.cs如下:

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

     routes.MapRoute(
      name: "Default_Localized", 
      url: "{culture}/{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      constraints: new { culture = new CultureConstraint(defaultCulture: "en", pattern: "[a-z]{2}") }, 
      namespaces: new string[] { "Locator.Areas.Location.Controllers" } 
     ).DataTokens["area"] = "Location"; 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new string[] { "Locator.Areas.Location.Controllers" } 
     ).DataTokens["area"] = "Location"; 
    } 
} 

注意,这将彻底取代默认控制器的任何功能在应用程序中,并将所有请求发送到Location命名空间。

您应该而不是将任何路径定义放入您的LocationAreaRegistration.cs文件中。这将确保他们运行最后,并且不要与任何其他区域路线拧紧。

这里是CultureConstraint的定义。有关如何本地化您的路线的更多详细信息,请参阅this answer

using System.Text.RegularExpressions; 
using System.Web; 
using System.Web.Routing; 

public class CultureConstraint : IRouteConstraint 
{ 
    private readonly string defaultCulture; 
    private readonly string pattern; 

    public CultureConstraint(string defaultCulture, string pattern) 
    { 
     this.defaultCulture = defaultCulture; 
     this.pattern = pattern; 
    } 

    public bool Match(
     HttpContextBase httpContext, 
     Route route, 
     string parameterName, 
     RouteValueDictionary values, 
     RouteDirection routeDirection) 
    { 
     if (routeDirection == RouteDirection.UrlGeneration && 
      this.defaultCulture.Equals(values[parameterName])) 
     { 
      return false; 
     } 
     else 
     { 
      return Regex.IsMatch((string)values[parameterName], "^" + pattern + "$"); 
     } 
    } 
} 
+0

谢谢:)它的工作 –