46

I need to create the following html output:

<a href="account/register?provider=1">Register</a>

How can I accomplish this with ASP.NET MVC 6 anchor tag helper? It seems that there is no option for the query string part. I want to use

<a asp-action="Register" asp-controller="Account">Register</a>

but where do I put "provider=1"?

1 Answer 1

84

Did you try the asp-route-* attribute:

<a asp-action="Register" 
   asp-controller="Account"
   asp-route-provider="1">
    Register
</a>
Sign up to request clarification or add additional context in comments.

4 Comments

Magic indeed. Great.
I don't believe this to be the right answer. This works fine for route parameters but if it is tagged as a [FromQuery] parameter on the argument of the controller method signature it does not get passed.
@Shawn, I have a [FromQuery] parameter on my action method, and this answer worked for me. I'm doing this: <a asp-action="Index" asp-route-embedded="true">Cancel</a> which gives me https://sitename/home/index?embedded=true.
This actually works, but I had to restart the app to make it work. Otherwise, the link wasn't generated with a proper query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.