gpt4 book ai didi

c# - 设置到区域的路由,而无需在 MVC 中的 url 中指定区域名称

转载 作者:太空狗 更新时间:2023-10-30 01:32:14 27 4
gpt4 key购买 nike

我有一个 MVC 4 应用程序。在该应用程序中,我有某些区域,其 URL 未正确解析。 LocationAreaRegistration.cs如下:

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

但我不希望 URL 包含 Location(区域名称)。我做错了什么?

编辑

我将要访问的 URL 有点像:

http://localhost/en/LocationIndex/LocationIndex

en 是当前文化,Homecontroller 名称,Index Action 方法名称。

最佳答案

要使 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";
}
}

请注意,这将完全替换应用程序中默认 Controller 的任何功能,并将所有请求发送到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 + "$");
}
}
}

关于c# - 设置到区域的路由,而无需在 MVC 中的 url 中指定区域名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37670809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com