ASP.NET Core MVC : Razor pages routing, incorrectly re-using previous route data
Asked Answered
T

2

0

I'm working on a dotnet 6 mvc application using RazorPages, and I'm having a problem with strange routing behavior.

I have a RazorPage /Pages/News.cshtml

This page is accessible using the default route /news

When called without any parameters this page will display an index of news articles.

I also want this page to be able to display a specific news article, via a path like this...

/news/1234-my-news-article

To achieve this, I've added a config like so...

builder.Services.AddRazorPages(options =>
{
    options.Conventions.AddPageRoute("/News", "News/{id:regex(^\\d+\\-.*)?}");
});

In my templates, I can then use links like this...

<a asp-page="/News" asp-all-route-data="@(new Dictionary<string, string> { { "id", "1234-my-news-article" } })">My News Article</a>

or

<a asp-page="/News">All Articles</a>

However, once I've navigated to a specific article, the index link doesn't render correctly, and will instead link again to the same article. It appears to be re-using the current routing parameters.

Is there some way to avoid this?

update:

I've found a work-around, if I use this tag instead...

<a asp-page="/News" asp-route-id="">All Articles</a>

then it will link correctly to "/news".

It seems a bit counter-intuitive to have to explicitly set this to blank. I would have assumed it would default to unset, unless explicitly set otherwise.

Thomson answered 6/7, 2022 at 3:1 Comment(2)
However, once I've navigated to a specific article, the index link doesn't render correctly, and will instead link again to the same article. It appears to be re-using the current routing parameters.Can you explain more about this?You will go to /news/1234-my-news-article with the anchor tag,and what you do in the page?Pulliam
@YiyiYou This tag <a asp-page="/News">All Articles</a> ordinarily renders to the anchor with the path "/news", however if I've linked to a specific news article, then the tag renders directly to that news article again, i.e. /News/1234-test-news-articleThomson
S
1

This is known as ambient route values (https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper#ambient-route-values), where the current route values are automatically added to outbound links which are generated by the anchor tag helper if the destination page is the same as the current page. In older versions of Razor Pages, this was the default behaviour for all pages.

As you have discovered, you can override this by setting the route value to an empty string, or as suggested elsewhere, to use a plain anchor tag for your link.

Sikora answered 6/7, 2022 at 6:17 Comment(0)
P
1

asp-page tag helper will add id value to the route by default.It is by design.If you don't want to add it,you can try to use href to replace asp-page:

<a href="/News">All Articles</a>
Pulliam answered 6/7, 2022 at 5:12 Comment(0)
S
1

This is known as ambient route values (https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper#ambient-route-values), where the current route values are automatically added to outbound links which are generated by the anchor tag helper if the destination page is the same as the current page. In older versions of Razor Pages, this was the default behaviour for all pages.

As you have discovered, you can override this by setting the route value to an empty string, or as suggested elsewhere, to use a plain anchor tag for your link.

Sikora answered 6/7, 2022 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.