C# 在MVC中设置到某个区域的路由,但不在url中指定区域名称

C# 在MVC中设置到某个区域的路由,但不在url中指定区域名称,c#,asp.net-mvc,asp.net-mvc-4,routes,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 4,Routes,Asp.net Mvc Routing,我有一个mvc4应用程序。在该应用程序中,我有一些区域的URL解析不正确。LocationAreaRegistration.cs如下所示: context.MapRoute( "Location_default", "{culture}/{controller}/{action}/{id}", new { culture = "en", action = "LocationIndex", id = UrlParameter.

我有一个
mvc4
应用程序。在该应用程序中,我有一些区域的URL解析不正确。
LocationAreaRegistration.cs
如下所示:

context.MapRoute(
            "Location_default",
            "{culture}/{controller}/{action}/{id}",
            new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional },
            new { controller = "(LocationIndex)" }
        );
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");
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";
    }
}
我的
route.config
如下所示:

context.MapRoute(
            "Location_default",
            "{culture}/{controller}/{action}/{id}",
            new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional },
            new { controller = "(LocationIndex)" }
        );
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");
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";
    }
}
我还尝试更改
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" }
           );
所有方法都不起作用,我得到
资源找不到
错误

但是,当我按如下方式更改
LocationAreaRegistration.cs
时,它会起作用:

context.MapRoute(
            "Location_default",
            "{culture}/Location/{controller}/{action}/{id}",
            new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional },
            new { controller = "(LocationIndex)" }
        );
但是我不希望URL包含
位置
(区域名称)。我做错了什么

编辑

我将要介绍的URL有点像:

http://localhost/en/LocationIndex/LocationIndex

这里的
en
是当前区域性,
Home
控制器
名称,
索引
操作方法
名称。

要使
位置
区域成为MVC应用程序的默认路由集,只需定义
RouteConfig.cs

context.MapRoute(
            "Location_default",
            "{culture}/{controller}/{action}/{id}",
            new { culture = "en", action = "LocationIndex", id = UrlParameter.Optional },
            new { controller = "(LocationIndex)" }
        );
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");
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";
    }
}
请注意,这将完全替换应用程序中默认控制器的任何功能,并将所有请求发送到
位置
命名空间

您不应将任何路线定义放入
LocationAreaRegistration.cs
文件中。这将确保它们是最后一个运行的,并且不会干扰任何其他区域路线

以下是
CultureConstraint
的定义。有关如何本地化路线的详细信息,请参阅

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 + "$");
        }
    }
}

这是一套很好的地图路线,但是在不知道你要去的URL的情况下,这个问题是无法回答的。@ErikPhilips:我已经编辑了我的问题。请检查您的路由配置是否错误。请参阅。@NightOwl888-是,如果我重定向到
http://localhost/en/Home/Index
。如果我重定向到
http://localhost/en/LocationIndex/LocationIndex
,则应将其重定向到区域位置中LocationIndex控制器的LocationIndex操作,但它不是重定向。附言-更新了我在question@Alorika-如果使用内置路由,URL必须包含路由可以识别为区域路由匹配区域的内容。可以是文字段或路由约束(可以使用反射只匹配存在的区域)。您说过不想使用文字段,但不清楚为什么这是不可接受的。您是否正在尝试将默认站点功能放入位置区域?