设置到区域的路由,而不在MVC的URL中指定区域名称

时间:2016-06-07 04:57:58

标签: c# asp.net-mvc asp.net-mvc-4 routes asp.net-mvc-routing

我有一个LocationAreaRegistration.cs申请。在该应用程序中,我有一些区域,其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

此处Home是当前文化,controllerIndex名称,action methodfunction get_categories() { $conn = db_connect(); $query = "select * from categories"; $result = $conn->query($query); if(!$result) { return false; }else { $result = db_result_to_array($result); return $result; } } 名称。

1 个答案:

答案 0 :(得分: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 + "$");
        }
    }
}