[英]ASP.NET MVC Routing with Areas & Languages not working
我有一个带有控制器的站点和另外两个带有各自控制器的区域。 该区域内的一名管制员有一个语言限制代码,例如 en。 默认情况下,它工作得很好。 但是,当我尝试在控制器中使用Route
规范时,它会以误导的方式构建路由。
RouteConfig.cs 文件如下所示
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.LowercaseUrls = true;
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "DefaultWithLanguageAndOrg",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyProj.Website.WebApp.Controllers" }
);
}
部分区域注册文件如下所示:-
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Test_default",
"{lang}/Test/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { lang = new LanguageRouteConstraint() },
new[] { "MyProj.Website.WebApp.Areas.Test.Controllers" }
);
}
controller 如下所示:-
[RouteArea("Test")]
[RoutePrefix("certificate")]
public class CertificationsController : Controller
{
[Route("Home")]
public ActionResult Home()
{
return View();
}
}
我的期望是拥有 URL 结构,例如site/en/Test/certificate/Home
,但我无法在 RouteArea 之前添加前缀en
。
笔记:-
[RouteArea("en/Test")]
它执行操作,但希望将视图文件夹移动到 en 中。 这不是一个合适的解决方案,没有 Route 规范的其他路由将无法工作。[RoutePrefix("{lang}/Test/certificate/Home")]
,它执行操作但不呈现视图。 它在路径中搜索视图,如~/Views/Certifications/Home.cshtml
缺少区域测试,它应该像~/Test/Views/Certifications/Home.cshtml
。 而且这种格式也[RoutePrefix("en/{area}/certificate")]
不走运。您可以覆盖 Controller 的 View()
喜欢 -
protected override ViewResult View(string ViewName, string masterName, object model)
{
return PrepareView(ViewName, masterName, model);
}
private ViewResult PrepareView(string ViewName, string masterName, object model)
{
renderview = base.View("~/Views/Shared/" + ViewName + ".cshtml", masterName, model);
return base.View(ViewName, masterName, model);
}
这只是一个示例代码,与此类似,您可以使用您的语言代码覆盖路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.