简体   繁体   English

重定向到另一个文件夹中的同一页面

[英]Redirect to the same page in another folder

How to redirect to the same page in another folder? 如何重定向到另一个文件夹中的同一页面?

I have a web site with two different languages. 我有一个使用两种不同语言的网站。 Can I redirect to the same page in the other language/folder if I click the language link button? 如果我单击语言链接按钮,是否可以使用其他语言/文件夹重定向到同一页面? Example: If I am in register page, when I click on the other language link button, I want to be redirected to the same page in the other language/folder. 示例:如果我在注册页面中,则当我单击另一种语言链接按钮时,我想重定向到另一种语言/文件夹的同一页面。

The main link of languages is in master page. 语言的主要链接在母版页中。

Is this possible? 这可能吗?

If I understand correctly, you're using subfolder to manage your culture. 如果我理解正确,则说明您正在使用子文件夹来管理您的文化。 So you'll have for exemple ~/en/Default.aspx and ~/fr/Default.aspx . 因此,例如~/en/Default.aspx~/fr/Default.aspx

If thats the case, you case use this piece of code. 如果是这样,您可以使用这段代码。

        //For exemple: /en/Default.aspx
        string currentURL = HttpContext.Current.Request.Url.AbsolutePath;

        //Manage different part of the URL
        string[] urlParts = currentURL.Split(new string[] {"/"},StringSplitOptions.RemoveEmptyEntries);

        //Remove the old culture code
        IEnumerable<string> invariantUrlParts = urlParts.Skip(1);

        //Rebuild the URL
        string newUrl = String.Format("~/{0}/{1}", "fr", String.Join("/", invariantUrlParts));

        //Redirect to ~/fr/Default.aspx
        Response.Redirect(newUrl);

EDIT 编辑

If you're a fan of one-liner: 如果您是单线的粉丝:

Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath
.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Aggregate(String.Format("~/{0}", "fr"), (i, j) => i + "/" + j));

Let's imagine you have a dropdown with all languages, and upon selection, you want to load the same page in the chosen language... 假设您有一个包含所有语言的下拉列表,并且在选择后想要以所选语言加载同一页面...

and assuming you know the current language (in a session variable for example) that is beeing loaded, you could have: 并假设您知道正在加载的当前语言(例如,在会话变量中),则可能有:

as dropdown: 作为下拉列表:

<select id="langSelector" onchange="changeCountry(this.value);">
  <option value="en">English</option>
  <option value="es">Spanish</option>
  <option value="fr">French</option>
</select>

in javascript: 在javascript中:

var currentLanguage = "en"; 
// for example in ASP.NET Webforms
// var currentLanguage = "<%= Session["currentLanguage"] %>";

function changeCountry(selectedLanguage) {
  var preLang = '/' + currentLanguage + '/',
      newLang = '/' + selectedLanguage + '/';  
  document.location = document.location.pathname.replace(preLangig, newLang);
}

explanation: 说明:

in javascript document.location.pathname will show the full path after the protocol and domain name, and if you have a simple /en/ you can replace that with /fr/ and load the rest again. 在javascript document.location.pathname将在协议和域名之后显示完整路径,如果您使用的是简单的/en/ ,则可以将其替换为/fr/然后再次加载其余部分。

to be fail proof, you need to do the replace ignoring the case, so En or eN could also be translated to fr . 为了防止故障,您需要忽略大小写进行replace ,因此EneN也可以转换为fr

in that cause you would use this: 因此,您将使用以下代码:

function changeCountry(selectedLanguage) {
  var newLang = '/' + selectedLanguage + '/';  
  document.location = 
     document.location.pathname.replace(
         /\/<%= Session["currentLanguage"].ToString().ToLower() %>\//i, 
         newLang);
}

so you would have: 因此您将拥有:

.replace(/\/<%= Session["currentLanguage"].ToString().ToLower() %>\//i, newLang);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM