Skip to content

Blazor-server-side Multi routing such as in mvc #37485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Alerinos opened this issue Oct 12, 2021 · 10 comments
Closed

Blazor-server-side Multi routing such as in mvc #37485

Alerinos opened this issue Oct 12, 2021 · 10 comments
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-blazor-server feature-routing

Comments

@Alerinos
Copy link

I have two sides

@page "/{article?}"

<h2>@article</h2>

@code {
    [Parameter]
    public string article { get; set; } = "";

    protected override async Task OnInitializedAsync() 
    {
        if(article != "testArticle")
        {
            // return NOTFOUND
        }
    }

}
@page "/{game?}"

<h2>@game</h2>

@code {
    [Parameter]
    public string game { get; set; } = "";

    protected override async Task OnInitializedAsync() 
    {
        if(game != "testGame")
        {
            // NOTFOUND
        }
    }

}

I would like many sites to be able to run under one domain like in mvc
example:

domain.com/good-news
dmoain.com/best-game
domain.com/good-forwarding
domain.com/testGame
domain.com/testArticle

in mvc it was enough to give return NotFound();

@javiercn
Copy link
Member

@Alerinos thanks for contacting us.

This is already possible today provided that you host your apps in different base paths. Here is a sample I wrote a while ago that you can use as a starting point. It's for Blazor webassembly, however you should be able to adapt it for Blazor server without much trouble.

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components feature-blazor-server feature-blazor-deployment Issues related to deploying Blazor labels Oct 12, 2021
@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Oct 12, 2021
@ghost ghost added the Status: Resolved label Oct 12, 2021
@Alerinos
Copy link
Author

I didn't really mean it. The application is shared, but large sites have several sections.
For example, a sports website may have articles, players and sports clubs.
Instead of doing

domain.com/club/Manchester-United -> domain.com/Manchester-United
domain.com/footballer/ronaldo ->domain.com/ronaldo
domain.com/article/best-match-ever-2020 -> domain.com/best-match-ever-2020

@javiercn I don't think your example will work that well

@javiercn
Copy link
Member

@Alerinos it's not clear to us what you are trying to achieve.

Are you looking for something similar to Areas in MVC?

@Alerinos
Copy link
Author

@javiercn
I am looking for a way for many pages to have a common dynamic link. Articles, clubs, footballers, coaches, stadiums. I want to do short links for SEO. Instead of creating a link with the module name, it's all directly from the database. When entering the page, one query flies, if it finds it, it returns the result, if not, it continues with the query. If you find something in the database, this page is displayed.

@javiercn javiercn added enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-routing and removed ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved feature-blazor-deployment Issues related to deploying Blazor labels Oct 14, 2021
@javiercn javiercn added this to the Backlog milestone Oct 14, 2021
@ghost
Copy link

ghost commented Oct 14, 2021

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@enetstudio
Copy link

@Alerinos, I believe that the Router's OnNavigateAsync feature can solve your issue.

Here's a simple code sample, checking the url path and navigating to the required url. This fulfills what you requested at the start of your question. I believe you can create a well-grained and complex navigation scheme. You can also query your database through Web Api end points, etc. See here the docs and follow the explanations:

App.razor

@inject NavigationManager NavigationManager

<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true" OnNavigateAsync="@OnNavigateAsync">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        <LayoutView Layout="@typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>
@code {

 
    private async Task OnNavigateAsync(NavigationContext context)
    {
        if (context.Path.ToLower() == "testArticle".ToLower())
        {
            NavigationManager.NavigateTo("counter");
        }
        else if (context.Path.ToLower() == "testGame".ToLower())
        {
            NavigationManager.NavigateTo("fetchdata");
        }

        await Task.CompletedTask;
    }
}```

Index.razor
------------

    page "/{MyParam?}"

    @code 
    {
         [Parameter]
        public string MyParam { get; set; } = "";
    }

I do hope that's what you were after. It seems to me that you may also  need to use the pattern suggested by javiercn above.

@Alerinos
Copy link
Author

Alerinos commented Feb 19, 2022

@enetstudio Unfortunately that won't work, it will only create a redirect. For this to work, you need to use the RenderHandle and the Render function

@pranavkm @javiercn Maybe it's worth taking a look at a custom router in .net 7? It is very useful.

@Alerinos
Copy link
Author

Alerinos commented Feb 19, 2022

Example:

app.UseEndpoints("/{name}",endpoints =>
{
    endpoints.MapControllerRoute(() => {
    if(db.ToDo.Any(x => x.Name == endpoints.name))
       RenderHandle.Redner(Pages.Counter)
    }
    );  
      
    endpoints.MapControllerRoute(() => {
    if(db.Weather.Any(x => x.ShortName == endpoints.name))
       RenderHandle.Redner(Pages.Fetchdata)
    }
    );        

  // OR
      endpoints.MapControllerRoute(() => {
    if(db.Weather.Any(x => x.ShortName == endpoints.name))
       RenderHandle.Redner(Pages.Fetchdata, endpoints.name) // Add One Get parameteres 
    }
    );        
}
);

Database:

ToDo
ID | Name
1 | oldToDo
2 | newToDo

Weather
ID | ShortName
1 | UK
2 | USA

If we type
domain.com/usa This will show us the page Pages.Fetchdata
or
domain.com/newToDo use page Pages.Counter

@Alerinos
Copy link
Author

Recently, I brought up this topic here #39719

@mkArtakMSFT
Copy link
Member

Hi. Thanks for contacting us.
We're closing this issue as there was not much community interest in this ask for quite a while now.
You can learn more about our triage process and how we handle issues by reading our Triage Process writeup.

@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Feb 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-blazor-server feature-routing
Projects
None yet
Development

No branches or pull requests

4 participants