gpt4 book ai didi

asp.net-mvc-routing - Response.RedirectToRoute(RouteData.Values)重定向到Area Controller

转载 作者:行者123 更新时间:2023-12-04 04:40:31 24 4
gpt4 key购买 nike

我正在尝试设置一个BaseController来处理文化作为url的一部分(基于ASP.NET MVC 5 Internationalization)。只要禁用我的区域注册,我的实现工作就可以正常进行。

注册我的区域之一后,如果我尝试输入错误/不受支持的区域性(http://localhost:52639/zz/),则会遇到请求URL http://localhost:52639/fr/Test/Post的404错误。

我已检查我的路线是否正确注册。

如果在禁用区域注册时执行相同的操作,则在键入以下URL时基本 Controller 和路由将正确运行:http://localhost:52639/zz/我被重定向到http://localhost:52639/fr/(默认区域性)。

,这些是我的路线:

public static void RegisterRoutes(RouteCollection routes)
{
var namespaces = new[]{typeof(PostController).Namespace};

routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("PostToHack", "{culture}/Post/{idAndSlug}", new { culture = "", Controller = "Post", Action = "Show" }, namespaces);
routes.MapRoute("Post", "{culture}/Post/{id}-{slug}", new { culture = "", Controller = "Post", Action = "Show" }, namespaces);

routes.MapRoute("TagToHack", "{culture}/Tag/{idAndSlug}", new { culture = "", Controller = "Post", Action = "Tag" }, namespaces);
routes.MapRoute("Tag", "{culture}/Tag/{id}-{slug}", new { culture = "", Controller = "Post", Action = "Tag" }, namespaces);

routes.MapRoute("Logout", "{culture}/Logout", new { culture = "", Controller = "Authentication", Action = "Logout" }, namespaces);
routes.MapRoute("Login", "{culture}/Login", new { culture = "", Controller = "Authentication", Action = "Login" }, namespaces);

//Error routes
routes.MapRoute("Error404", "{culture}/errors/404", new { culture = "", Controller = "Errors", Action = "NotFound" }, namespaces);
routes.MapRoute("Error500", "{culture}/errors/500", new { culture = "", Controller = "Errors", Action = "Error" }, namespaces);

routes.MapRoute("Home", "{culture}", new { culture = "", Controller = "Post", Action = "Index"},namespaces);

//Never to be called by user which is why it comes after MapRoute Home so it is always overwritten by it
routes.MapRoute("Sidebar", "{culture}", new { culture = "", Controller = "Layout", Action = "Sidebar"},namespaces);//This is a "child-only" controller
routes.MapRoute("NavigationBar", "{culture}", new { culture = "", Controller = "Layout", Action = "NavigationBar"},namespaces);//This is a "child-only" controller

区域路线
 public override void RegisterArea(AreaRegistrationContext context)
{
var namespaces = new[] { typeof(PostsController).Namespace };

context.MapRoute(
"admin_default",
"{culture}/admin/{controller}/{action}/{id}",
new { culture = "", action = "Index", id = UrlParameter.Optional }, namespaces
);
}

基本 Controller :
 public class BaseController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
var cultureName = RouteData.Values["culture"] as string;

// Attempt to read the culture cookie from Request
if (cultureName == null)
cultureName = (Request.UserLanguages != null) && (Request.UserLanguages.Length > 0)
? Request.UserLanguages[0]
: null; // obtain it from HTTP header AcceptLanguages

// Validate culture name
cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe


if (RouteData.Values["culture"] as string != cultureName)
{
// Force a valid culture in the URL
RouteData.Values["culture"] = cultureName.ToLowerInvariant(); // lower case too

// Redirect user
Response.RedirectToRoute(RouteData.Values);
}

// Modify current thread's cultures
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

return base.BeginExecuteCore(callback, state);
}
}

最佳答案

经过更多的挖掘之后,我找到了适合我的解决方案。我的问题来自于我的路线注册顺序。我首先注册了我所在地区的路线:

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

我颠倒了订单,并确保只在以下时间注册我所在地区的路线:
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
}

关于asp.net-mvc-routing - Response.RedirectToRoute(RouteData.Values)重定向到Area Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39380005/

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