Bjørn
Bjørn

Reputation: 1148

Reuse old aspx routes in Blazor

We are writing a new version of an old page that other pages are dependent on. As such it's important to keep old routing.

One of the routes is as follows:

https://example.com/area/page.aspx?id=123

I would like for this to work, but point to a blazor component. I have tried adding it as a @page supplement to the ones I already have, but the application wont run with it.

@page "/page/{id}/{property:int?}"
@page "/page/{id}"
@page "/area/page.aspx?id={id}"

I have also tried just

@page "/area/page.aspx

but this seems not to work either. Blazor isn't even starting when I do this. I also tested

@page "/area/page.tst

to no avail. I believe that it's the . and ? that is the culprits.

A solution might be to redirect from the old URL to the component, with something that the @page directive can handle, but I can't figure out where in the middleware this would be done.

Question is basically: Which ways one can reuse old URL's with Blazor?

Upvotes: 0

Views: 383

Answers (1)

Bjørn
Bjørn

Reputation: 1148

I found that in Startup.cs there's a method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

That has this in it:

app.UseEndpoints(endpoints =>
{
    endpoints.MapBlazorHub();
    endpoints.MapFallbackToPage("/_Host");
    endpoints.MapControllers();
});

Here I could just add the following line:

endpoints.MapGet("/area/page.aspx", async x => { x.Response.Redirect("~/page/"); });

And it would redirect. Nifty :)

Resulting code would then be:

app.UseEndpoints(endpoints =>
{
    endpoints.MapBlazorHub();
    endpoints.MapFallbackToPage("/_Host");
    endpoints.MapControllers();
    endpoints.MapGet("/area/page.aspx", async x => { x.Response.Redirect("~/page/"); });
});

Upvotes: 1

Related Questions