asp.net – 使用MVC的Attribute Routing和RouteLocalization.mvc
我希望能够为我的网站创建一个简洁的特定于语言的默认URL,以便当有人浏览时:
somesite.com 他们被重定向到语言文化页面,例如: > somesite.com/en-US/ 具体来说,我不希望/ Home / Index附加到URL: > somesite.com/en-US/Home/Index 我致力于使用RouteLocalization.mvc创建此站点 > Dresel/RouteLocalization 我想在可行的范围内使用MVC属性路由. 我无法弄清楚如何使Start()方法重定向到特定于语言文化的URL而不添加像“index”这样的东西. 我试图遵循的样本: using RouteLocalization.Mvc; using RouteLocalization.Mvc.Extensions; using RouteLocalization.Mvc.Setup; public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Clear(); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(Localization.LocalizationDirectRouteProvider); const string en = "en-us"; ISet<string> acceptedCultures = new HashSet<string>() { en,"de","fr","es","it" }; routes.Localization(configuration => { configuration.DefaultCulture = en; configuration.AcceptedCultures = acceptedCultures; configuration.AttributeRouteProcessing = AttributeRouteProcessing.AddAsNeutralAndDefaultCultureRoute; configuration.AddCultureAsRoutePrefix = true; configuration.AddTranslationToSimiliarUrls = true; }).TranslateInitialAttributeRoutes().Translate(localization => { localization.AddRoutesTranslation(); }); CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate = Localization.DetectCultureFromBrowserUserLanguages(acceptedCultures,en); var defaultCulture = System.Threading.Thread.CurrentThread.CurrentUICulture.Name; routes.MapRoute( name: "DefaultLocalized",url: "{culture}/{controller}/{action}/{id}",constraints: new { culture = @"(w{2})|(w{2}-w{2})" },defaults: new { culture = defaultCulture,controller = "Home",action = "Index",id = UrlParameter.Optional } ); routes.MapRoute( name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",id = UrlParameter.Optional } ); } } 和我的家庭控制器: public class HomeController : Controller { [HttpGet] [Route] public RedirectToRouteResult Start() { return RedirectToAction("Home",new { culture = Thread.CurrentThread.CurrentCulture.Name }); } [Route("Index",Name = "Home.Index")] public ActionResult Index() { return View(); } public ActionResult Contact() { return View(); } public ActionResult About() { return View(); } } 我的Global.asax文件: public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); AreaRegistration.RegisterAllAreas(); } } 解决方法
重定向是一个单独的问题而不是路由.由于您将任何URL重定向到其本地化对象的目标是一个跨领域的问题,因此您最好的选择是制作全局过滤器.
public class RedirectToUserLanguageFilter : IActionFilter { private readonly string defaultCulture; private readonly IEnumerable<string> supportedCultures; public RedirectToUserLanguageFilter(string defaultCulture,IEnumerable<string> supportedCultures) { if (string.IsNullOrEmpty(defaultCulture)) throw new ArgumentNullException("defaultCulture"); if (supportedCultures == null || !supportedCultures.Any()) throw new ArgumentNullException("supportedCultures"); this.defaultCulture = defaultCulture; this.supportedCultures = supportedCultures; } public void OnActionExecuting(ActionExecutingContext filterContext) { var routeValues = filterContext.RequestContext.RouteData.Values; // If there is no value for culture,redirect if (routeValues != null && !routeValues.ContainsKey("culture")) { string culture = this.defaultCulture; var userLanguages = filterContext.HttpContext.Request.UserLanguages; if (userLanguages.Length > 0) { foreach (string language in userLanguages.SelectMany(x => x.Split(';'))) { // Check whether language is supported before setting it. if (supportedCultures.Contains(language)) { culture = language; break; } } } // Add the culture to the route values routeValues.Add("culture",culture); filterContext.Result = new RedirectToRouteResult(routeValues); } } public void OnActionExecuted(ActionExecutedContext filterContext) { // Do nothing } } 用法 public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new RedirectToUserLanguageFilter("en",new string[] { "en","it" })); filters.Add(new HandleErrorAttribute()); } } 另请注意,您的路由配置错误.每个应用程序启动时路由设置运行一次,因此将默认文化设置为当前线程的文化是没有意义的.实际上,您不应该为您的文化路径设置默认文化,因为您希望它丢失,因此如果没有设置文化,您的默认路由将会执行. routes.MapRoute( name: "DefaultLocalized",id = UrlParameter.Optional } ); routes.MapRoute( name: "Default",id = UrlParameter.Optional } ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET Repeater ItemDataBound事件中等效的Eval(“field”
- asp.net-mvc – 应该在ASP.NET MVC应用程序中缓存发生在哪里
- asp.net – FormsAuthentication.GetRedirectUrl始终返回默
- .net – 任何具有Postsharp生产经验的人?
- asp.net-mvc – Web API IE9 JSON数据“你想打开还是保存这
- asp.net-mvc-3 – 是否可以在Javascript文件中使用razor语法
- asp.net-mvc-4 – 添加对System.IdentityModel.Tokens DLL的
- asp.net – 如何创建自定义数据注释验证器
- asp.net-mvc-3 – 哪里和如何定义我的asp.net MVC 3 web应用
- 如何返回404状态,无效参数传递给我的ASP.NET MVC控制器?
- ASP.NET Application在不使用Global.asax的情况下
- asp.net – Web部署错误到IIS – 应用程序池mana
- asp.net-mvc – 更新用户声明不起作用.为什么?
- asp.net – 使用ItemDataBound我只获得每隔一行.
- 在没有viewstate的情况下运行ASP.NET
- asp.net-mvc – IIS通过http方法重写排除规则
- asp.net-mvc – mvc视图中的条件逻辑vs htmlhelp
- 如何在ASP.NET中获取服务器端的输入值(类型文本)
- 缺少“ASP.NET核心Web应用程序(.NET Framework)”
- ASP.NET – meta:ResourceKey vs <%$Resources