【问题标题】:Router cannot find route when sending with parameter带参数发送时路由器找不到路由
【发布时间】:2019-01-28 11:12:54
【问题描述】:

我正在尝试使用参数导航到page,但不知何故它似乎不起作用。 我得到了错误:

blazor.webassembly.js:1 Uncaught (in promise) Error: System.InvalidOperationException: 'Router' cannot find any component with a route for '/home/+HhbuYzG1j5j/lzjL2gUUYdA5HgM0KYVTx55NYBuYLkYyMOImyVT8627J1iRAkCxjY2ZOAtoA/PuL2dePKX8+qSngAsymb6k010UIk8IG+p7/SlX2PTKEtyVVUXWqgstdmaIn95MXyV0w1XEHYXHm3rv1L5CwIicFtg8b8Vpkng='.
  at Microsoft.JSInterop.DotNetDispatcher.InvokeSynchronously (:8300/System.String assemblyName, System.String methodIdentifier, System.Object targetInstance, System.String argsJson) <0x2a4a160 + 0x001bc> in <af36ed9192be4a95b907125ccf0c1e59>:0 
  at Microsoft.JSInterop.DotNetDispatcher.BeginInvoke (:8300/System.String callId, System.String assemblyName, System.String methodIdentifier, System.Int64 dotNetObjectId, System.String argsJson) <0x2a49d08 + 0x00088> in <af36ed9192be4a95b907125ccf0c1e59>:0 
    at endInvokeDotNetFromJS (blazor.webassembly.js:1)

发件人

@page "/"
@using Microsoft.AspNetCore.Blazor.Services;
@inherits AuthBase

<input type="text" bind="@Username" />
<button type="button" onclick="@AuthAsync">Authenticate</button>

发送者逻辑

 public class AuthBase : BlazorComponent {
        [Inject]
        protected IUriHelper uriHelper { get; set; }
        [Inject]
        protected AuthService authService { get; set; }


        public string Username { get; set; }
        public string url = "/home";

        public async Task AuthAsync() {
            var ticket = await this.authService.AuthenticateAsync(Username);
            if (ticket.Value == string.Empty) {
                Console.WriteLine("Ticket invalid,retry !");
                return;
            }
            var urlToSend = url + "/" + ticket.Value;
            Console.WriteLine("Sending to:" + urlToSend); 
            uriHelper.NavigateTo(urlToSend);
        }
    }

接收者

@page "/home/{Ticket}"
@inherits HomeBase

接收器逻辑

 public class HomeBase:BlazorComponent {

    [Parameter]
    private string Ticket { get; set; }
}

我不明白我希望被调用的 url 是 /home/[ticketvalue],从错误中可以看出它是正确的。

为什么它不起作用?

【问题讨论】:

    标签: c# routing blazor


    【解决方案1】:

    问题是您的Ticket 值包含斜杠/

    【讨论】:

    • 我使用Uri.EscapeDataString 解决了它。非常感谢,我完全错过了。
    猜你喜欢
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2016-07-03
    • 2013-06-14
    • 1970-01-01
    • 2018-11-24
    相关资源
    最近更新 更多