English 中文(简体)
页: 1
原标题:asp.net mvc multilanguage urls/routing

这是关于Ap.net mvc多种语言的urls/routing和SEO最佳做法/收益的两个部分问题。

www.un.org/spanish/ecosoc

我被要求设立一个新的伙伴关系。 NET MVC网站将支持两种语文(英文和法文)中至少三种语文(第一)的内容。

在将申请(实验室、酒类错误等)本地化方面,应当使用资源档案,我发现这方面的许多例子。 但是,我的关切/问题更多地涉及危民盟。

就地轨道而言,这两种方式之间所建议的做法是什么?

Fashion 1 (no culture folder)  
www.mydomain.com/create-account 
www.mydomain.com/creer-un-compte

Fashion 2 (with built in culture folder)
www.mydomain.com/create-account 
www.mydomain.com/fr/creer-un-compte <--notice the “fr” folder 

是否使用过一种问题/罚款?

还是如此之少,它变得无关紧要!


To achieve Fashion 2, I’ve already found an article here: ASP.NET MVC - Localization route

But I’d be curious to find how to achieve Fashion 1.

是否有任何联系?

此外,据我所知,URL Rebecca is not,“我所期待的东西”,因为我不想“直接”用户...... 我只想用适当的语言展示,而不必在导游中展示文化。

预先感谢在这方面的任何帮助。

最佳回答

你们可以创建具有以下地方化逻辑的基础控制器:

public abstract class LocalizedController : Controller
{
  protected override void ExecuteCore()
  {
    HttpCookie cookie;
    string lang = GetCurrentCulture();
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang, false);

    // set the lang value into route data
    RouteData.Values["lang"] = lang;

    // save the location into cookie
    cookie = new HttpCookie("DPClick.CurrentUICulture",
    Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName)
    {
      Expires = DateTime.Now.AddYears(1)
    };

    HttpContext.Response.SetCookie(cookie);  
    base.ExecuteCore();
  }

  private string GetCurrentCulture()
  {
    string lang;

    // set the culture from the route data (url)

    if (RouteData.Values["lang"] != null &&
      !string.IsNullOrWhiteSpace(RouteData.Values["lang"].ToString()))
    {
      lang = RouteData.Values["lang"].ToString();
      if (Localization.Locales.TryGetValue(lang, out lang))
      {
        return lang;
      }
    }

    // load the culture info from the cookie
    HttpCookie cookie = HttpContext.Request.Cookies["DPClick.CurrentUICulture"];
    if (cookie != null)
    {
      // set the culture by the cookie content
      lang = cookie.Value;
      if (Localization.Locales.TryGetValue(lang, out lang))
      {
        return lang;
      }
    }

    // set the culture by the location if not speicified
    lang = HttpContext.Request.UserLanguages[0];
    if (Localization.Locales.TryGetValue(lang, out lang))
    {
      return lang;
    }

    //English is default
    return Localization.Locales.FirstOrDefault().Value;
  }
}

The above controller satisfy part 2 of your question if you want to ignore the culture folder just don t assign the lang in the RouteData. Of course to achieve part 2 you have to add routing for culture as below:

routes.MapRoute(
  "Localization", // Route name
  "{lang}/{controller}/{action}/{id}", // URL with parameters
  new {controller = "Default", action = "Index", id = UrlParameter.Optional}, // Parameter defaults
  new {lang = @"w{2,3}(-w{4})?(-w{2,3})?"}
);
问题回答

实现你们所希望的三项基本需要:

A multi-language aware route to handle incoming URLs:

routes.MapRoute(
    name: "DefaultLocalized",
    url: "{lang}/{controller}/{action}/{id}",
    constraints: new { lang = @"(w{2})|(w{2}-w{2})" },   // en or en-US
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

www.un.org/french/ga/president

public class LocalizationAttribute : ActionFilterAttribute
{
    private string _DefaultLanguage = "en";

    public LocalizationAttribute(string defaultLanguage)
    {
        _DefaultLanguage = defaultLanguage;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string lang = (string)filterContext.RouteData.Values["lang"] ?? _DefaultLanguage;
        if (lang != _DefaultLanguage)
        {
            try
            {
                Thread.CurrentThread.CurrentCulture =
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
            }
            catch (Exception e)
            {
                throw new NotSupportedException(String.Format("ERROR: Invalid language code  {0} .", lang));
            }
        }
    }
}

www.un.org/Depts/DGACM/index_spanish.htm 一种在您申请中产生这些“URLs。 Html.ActionLink和Url.Action 接受一个<代码>CultureInfo作为参数的物体(和/或使用CultureInfo.CurrentCulture作为缺省之一),例如:

(both are written in C#)

您也可以避免推广方法模式,将其写成<条码>。 MultiLanguageActionLink/.MultiLanguageAction

欲了解更多关于此专题的信息和进一步样本,也可读到http://www.ryadel.com/en/setup-a-multi-language-website-using-asp-net-mvc/"rel=“nofollow noreferer”>>,在我的博客上张贴

补习可满足时间要求:

AttributeRouting - Localization





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签