Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Tag helper remembering URL parameter #6420

Closed
chrislamb139 opened this issue Jun 21, 2017 · 6 comments
Closed

Tag helper remembering URL parameter #6420

chrislamb139 opened this issue Jun 21, 2017 · 6 comments

Comments

@chrislamb139
Copy link

Hi,
I've created a new blank ASP.NET Core Web Application website using and added the following code to the generated About page:

<a asp-controller="Home" asp-action="About" asp-route-id="1234">Home 1234</a>
<br />
<a asp-controller="Home" asp-action="About">Normal Home</a>
<br />
<a asp-controller="Home" asp-action="About" asp-route-id="">Home with empty Id</a>
<br />
<a href="/Home/About">Home static url</a>

If I click on the 1st link I get navigated to a page with a URL of 'Home/About/1234', which is fine.
If I then click on the 2nd link I then get navigated to a page with a URL of 'Home/About/1234'. But in this case I'd expect it to go to 'Home/About/' as nothing in that hyperlink contains '1234'.
The 3rd and 4th links work as I'd expect, going to 'Home/About/'. However, I don't understand why I'd need to explicitly say asp-route-id='' in order to make it blank.

Can anyone shed some light on what's going on here and provide a solution?

Thanks,
Chris

@rynowak
Copy link
Member

rynowak commented Jun 21, 2017

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing#url-generation-reference

This should explain it, if not let us know and we'll improve the docs.

Basically, routing attempts to remember and reuse the route values from the current request. It doesn't know the difference between route values that take you to a different page -- controller, action, and those that don't -- id.

It works lexically right to left.

@chrislamb139
Copy link
Author

Ah ok, that makes sense.
In my application I'm building my navigation links like the one in the 2nd link. The issue is that after navigating to the page with a /something/1234. The user would expect that when they click on that same link again that it will take them back to /something/.

Should I not be building my navigation links like that? Should I instead be using something like the 3rd or 4th links in my example?

@rynowak
Copy link
Member

rynowak commented Jun 21, 2017

I'd recommend example 3 above, just blank out the id if you want to drop it.

@nurdyguy
Copy link

I ran across this issue with form action route creation. In my case, my get controller action accepts a route variable (often a GUID) which was then being added to the form's posting url, not ideal. The solution offered here seems to contradict the "Tell, don't Ask" principle. In any event, a note about this might be helpful on this page: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms

@Eilon
Copy link
Member

Eilon commented Jun 22, 2017

@Eilon Eilon closed this as completed Jun 22, 2017
@Allann
Copy link

Allann commented Jul 7, 2017

This appears to be a dangerous hidden side-effect in my opinion.
As @chrislamb139 mentions, he clicks on one link (1st request, HttpGet About(id?=1234), then the 2nd link (a brand new 2nd request, HttpGet About()), why should the 2nd request use an "ambient value" for an optional id from the first request? This would mean that id isn't really optional at all and is actually required to be able to go back to the default About page without the id. This seems wrong to me.

How would you clear "ambient values" from the route "state" then without having to specifically specify the id?

I ask as there may be many other optional parameters that could potentially also be set causing completely unwanted behaviour, and without knowing I have to "null" these values in a request I could perform unwanted requests. Does that make sense?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants