【问题标题】:Blazor with php style url parameters ( ?parameter= )带有 php 样式 url 参数的 Blazor ( ?parameter= )
【发布时间】:2022-01-01 15:12:22
【问题描述】:

我正在使用他们的身份验证 API 为 Envato 构建一个 Web 应用程序

基本上当用户使用 Envato 登录时,他们会被 Envato 重定向到

myapplink.com?code=1234-qwerty-login-code

在我的应用程序端,我应该使用此代码参数向 API 发送请求,以获取该用户的访问令牌。

我是 Blazor(服务器)的新手,我知道并且尝试访问 Blazor url 参数的唯一方法是通过 myapplink.com/login/parameter 我尝试使用 php 样式参数(例如 myapplink.com/login/?parameter=value),但它似乎是无法识别。

在我的剃须刀组件中

Authentication.razor

@page "/authentication/{code?}"

<PageTitle>APP | Authentication</PageTitle>

<div>Some html to render the component</div>

组件基础类似于

Authentication.razor.cs

public partial class Authentication
{
    [Parameter]
    public string? Code { get; set; }
    
    private async Task Authenticate()
    {

    }
}

我可以使用访问参数

myapplink.com/authentication/code-123-qwerty

我的问题:如何使用 PHP 样式的 url 参数访问参数 (myapplink.com/login/?cose=value)

【问题讨论】:

    标签: blazor blazor-server-side asp.net-blazor


    【解决方案1】:

    您需要在 Razor 组件中解析查询字符串。路由指令必须是

    @page "/authentication"
    

    在OnInitialized 方法中,您必须从传递给页面的查询字符串中获取值。

    查看https://chrissainty.com/working-with-query-strings-in-blazor/以获取详细信息。

    【讨论】:

    • 这正是我想要的,谢谢你的帮助?
    【解决方案2】:

    基于Farzan Hajian的回答,

    我能够按照以下步骤解决它:

    1. 使用 nuget 包管理器安装 Microsoft.AspNetCore.WebUtilities
    2. 将@inject NavigationManager NavManager 添加到blazor component
    @page "/authentication/{code?}"
    @inject NavigationManager NavManager
    
    <PageTitle>App | Authentication</PageTitle>
    
    1. 最后得到code参数OnInitialized
        [Parameter]
        public string? Code { get; set; }
        protected override void OnInitialized()
        {
            var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
            if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("code", out var _code))
            {
                Code = Convert.ToString(_code);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-11-11
      • 2011-10-31
      • 1970-01-01
      • 2023-04-01
      • 2021-10-29
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 2021-05-23
      相关资源
      最近更新 更多