如何在ASP.NET Core Razor Pages中动态添加属性/参数到URL?

huangapple go评论67阅读模式
英文:

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:

如何在ASP.NET Core Razor Pages中动态添加属性/参数到URL?

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:

如何在ASP.NET Core Razor Pages中动态添加属性/参数到URL?

> 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();
    }

huangapple
  • 本文由 发表于 2023年5月7日 23:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194731.html
  • asp.net-core-7.0
  • c#
  • razor-pages

为所有缺失的枚举添加到List中。 go 73
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码