简体   繁体   中英

Can't add Route to Blazor Server Side

Ill try to register route via

services.AddRazorPages(options => options.Conventions.AddPageRoute("/counter", "/c"));

in ConfigureServices .

I can't just add @page "/c" , because I plan to pull up this addresses from the config file.

But, when I try to visit .../c , it says that the page was not found, .../counter works. What could be the problem?

You may have a valid reason for resorting to AddPageRoute , but you haven't said, so why don't you just do this:

@page "/counter"
@page "/c"
@page "/co"
@page "/cou"

<h1>Counter</h1>

Unlike Razor, Blazor takes multiple @page directives.

RazorPages.= Blazor.

Your route does not apply to Blazor pages.

When there was an .AddRazorPages() in the original template code then that was from using Individual Accounts. You can add a route for the Login, Logout and Account pages this way. Not for /Counter.

i want load address from config file and make redirect from this address to static page

I made a demo for you:

@page "/"
@inject NavigationManager NavigationManager
@inject Microsoft.Extensions.Configuration.IConfiguration config

<button class="btn-primary" @onclick="RedirectTo">Go</button>

@code {
    void RedirectTo()
    {
        var address = @config["address"];
        NavigationManager.NavigateTo(address);
    }

//Use this if you want to redirect immediately without some trigger event
    protected override void OnInitialized()
    {
        var address = @config["address"]; //get address from config file
        NavigationManager.NavigateTo(address); //redirect
    }
}

Address configs in appsettings.json as an example:

{
  "address": "/counter",
  "Logging":...,
  "AllowedHosts": "*"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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