英文:
How to add attribute/parameters to the url in ASP.NET Core Razor Pages dynamically?
问题
If the user changes or enters the URL manually, I want to add the attribute "culture" to the URL.
For example, if the user wants to go to the "post" page and manually changes the URL to "https://localhost:4432/post," then I want to add the attribute "?culture=" to the URL. I wrote this code in the "post.cshtml.cs" class, but it doesn't work:
public IActionResult OnGet()
{
string culture = "fa";
if (Request.Cookies.ContainsKey("_culture"))
culture = Request.Cookies["_culture"].ToString();
Response.Redirect("/post?culture=" + culture);
return Page();
}
The result is here:
This page isn't working:
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
英文:
If the user change or enter the url manually ,I want to add attribute "culture" to the url .
For example the user wants to go to the "post" page and change the url to the "https://localhost:4432/post" manually,then I want to add attribute "?culture=" to the url,I write this code in "post.cshtml.cs" class but it doesn't work :
public IActionResult OnGet()
{
string culture = "fa";
if (Request.Cookies.ContainsKey("_culture"))
culture = Request.Cookies["_culture"].ToString();
Response.Redirect("/post?culture=" + culture);
return Page();
}
The result is here:
> This page isn't working:
> localhost redirected you too many times.
> Try clearing your cookies.
> ERR_TOO_MANY_REDIRECTS
答案1
得分: 1
You have to break out of the infinite loop of the redirect. If
the requested URL does not have the query tag for culture then add it with the redirect and return the page. Else
just return the page.
public IActionResult OnGet()
{
string culture = Request.Query["culture"];
if (String.IsNullOrEmpty(culture))
{
culture = "fa";
if (Request.Cookies.ContainsKey("_culture"))
culture = Request.Cookies["_culture"].ToString();
Response.Redirect("/post?culture=" + culture);
}
return Page();
}
英文:
You have to break out of the infinite loop of the redirect. If
the requested URL does not have the query tag for culture then add it with the redirect and return the page. Else
just return the page.
public IActionResult OnGet()
{
string culture = Request.Query["culture"];
if (String.IsNullOrEmpty(culture))
{
culture = "fa";
if (Request.Cookies.ContainsKey("_culture"))
culture = Request.Cookies["_culture"].ToString();
Response.Redirect("/post?culture=" + culture);
}
return Page();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论