【问题标题】:ASP.Net MVC: How to add country code to all urlASP.Net MVC:如何将国家代码添加到所有 url
【发布时间】:2016-11-19 18:38:09
【问题描述】:

我有两个要求

第一个是:我的要求是在所有网址中添加国家代码。在我的网址看起来像之前

www.mysite.net/catalog.aspx

www.mysite.net/product.aspx

www.mysite.net/cart.aspx

但现在我必须在全局位置编写一些代码,它将在所有 url 中注入 2 个字符国家代码,只是读取 cookie 值。所以我的新网址看起来像

www.mysite.net/uk/catalog.aspx

www.mysite.net/uk/product.aspx

www.mysite.net/uk/cart.aspx

所以告诉我我需要在网站的全球区域添加哪些代码,这些代码会在 url 中注入国家代码。

第二个是:如果国家代码将被注入浏览器地址栏中的url,那么我所有页面中的所有链接都应该具有相同的国家代码。

如果可能的话,用代码示例和提示讨论这个问题。一个人告诉我下载并安装 IIS URL Rewrite 模块,但我不知道 IIS IIS URL Rewrite 模块如何注入从国家 cookie 读取的国家代码。我想我需要为此添加一些代码。

我可以通过asp.net mvc中的url路由解决它吗?寻求帮助。

谢谢

【问题讨论】:

  • 如果 URL 中有文化,则不需要 cookie。您不会将文化注入到 URL 中,用户通过 URL 请求文化。请参阅:ASP.NET MVC 5 culture in route and url
  • 我的网站有很多国旗。因此,当用户单击任何国家/地区标志时,我们会将带有国家/地区代码的 cookie 放入用户 PC 中,并根据国家/地区代码显示本地化数据,但是当用户第一次来时,则不存在 cookie,默认国家/地区变为 GB。
  • 但这并不意味着您需要 cookie。如果您需要本地化的信息已经在 URL 中,您就不需要它(也不应该使用它)。本地化是内容,而不是个性化。您应该将每种文化放在 URL 中,以便搜索引擎可以用本地语言对其进行索引,而不是将其隐藏在搜索引擎永远不会提供的 cookie 后面。
  • 我的网站是基于 cookie 的。基本上我有一个使用 webform 开发的父网站,现在我必须使用 mvc 开发另一个小型网站,它将在我的主网站下运行并从我的主网站读取 cookie。如果未找到 cookie,则 GB 将是默认国家/地区。当我从 IDE 启动我的 mvc 站点时,我需要做什么,然后基于 cookie 值的默认 url 看起来像 localhost:2010/uk 或 localhost:2010/de ?给我一些想法。
  • 用户只需请求一个 URL,整个网站就会切换到该文化。您需要做的就是制作一个简单的控件来在 URL 之间切换,然后工作就完成了(比使用 cookie 更简单,也比使用 cookie 更好)。一旦请求了一种文化,只要您使用路由生成 URL,它就会“坚持”直到请求另一种文化。

标签: asp.net-mvc asp.net-mvc-routing url-rewrite-module


【解决方案1】:

这样我就完成了工作。所以我喜欢分享可能对其他人有帮助的东西。

我的 Application_BeginRequest 看起来像

 protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            string CountryCodeInUrl = "", redirectUrl="";
            var countryCode = CookieSettings.ReadCookie();
            if (countryCode=="")
            {
                countryCode = "gb";
            }

            if (HttpContext.Current.Request.RawUrl.Length >= 2)
            {
                CountryCodeInUrl = HttpContext.Current.Request.RawUrl.Substring(1, 2);
            }

            if (countryCode != CountryCodeInUrl)
            {
                if (HttpContext.Current.Request.RawUrl.Length >= 2)
                {
                    if (HttpContext.Current.Request.RawUrl.Substring(1, 2) != "")
                    {
                        countryCode = HttpContext.Current.Request.RawUrl.Substring(1, 2);
                    }
                }

                if(!System.Web.HttpContext.Current.Request.RawUrl.Contains(countryCode))
                {
                    redirectUrl = string.Format("/{0}{1}", countryCode, System.Web.HttpContext.Current.Request.RawUrl);
                }
                else
                {
                    redirectUrl = System.Web.HttpContext.Current.Request.RawUrl;
                }
                CookieSettings.SaveCookie(countryCode);
                System.Web.HttpContext.Current.Response.RedirectPermanent(redirectUrl);
            }

        }

CookieSettings 类

public class CookieSettings
    {
        public static void SaveCookie(string data)
        {
            var _CookieValue= new HttpCookie("CountryCookie");
            _CookieValue.Value = data;
            _CookieValue.Expires = DateTime.Now.AddDays(300);
            HttpContext.Current.Response.Cookies.Add(_CookieValue);
        }

        public static string ReadCookie()
        {
            var _CookieValue = "";
            if (HttpContext.Current.Request.Cookies["CountryCookie"] != null)
                _CookieValue= HttpContext.Current.Request.Cookies["CountryCookie"].Value;
            return _CookieValue;
        }
    }

例程配置如下

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "DefaultCountry",
                url: "{countrycode}/{controller}/{action}/{id}",
                defaults: new {countrycode="gb", controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Home 控制器索引操作看起来像

public ActionResult Index(string countrycode)
{
    return View();
}

这个问题已经结束了。

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 2021-02-17
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多