Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

asp.net - Appending Route parameter to href

I am a beginner with ASP.NET and i am trying to append a parameter to an href link.

I am not really sure how to get the href to look like https://www.itelde.com/flick?id=113

HTML

<a href="https://www.itelde.com/flick?id=" asp-route-id="@Model" target="_blank">
    Flicker
</a>

Controller

public async Task<IActionResult> Flicker([BindRequired, FromQuery] string id)
{
    return id;
}
question from:https://stackoverflow.com/questions/65859201/appending-route-parameter-to-href

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are setting the href itself directly, then you can just concat the things here:

<a href="https://www.itelde.com/flick?id=@Model" target="_blank">Flicker</a>

The asp-route-id tag helper can be used when you are generating the href by using asp-page or asp-action to refer to a Razor page or a controller action within your application. For example, if that Flicker action is part of your app, you could write it like this:

<a asp-action="Flicker" asp-route-id="@Model" target="_blank">Flicker</a>

If you try to use asp-route-something with an explicit href, you should also get the following error at runtime:

InvalidOperationException: Cannot override the 'href' attribute for <a>. An <a> with a specified 'href' must not have attributes starting with 'asp-route-' or an 'asp-action', 'asp-controller', 'asp-area', 'asp-route', 'asp-protocol', 'asp-host', 'asp-fragment', 'asp-page' or 'asp-page-handler' attribute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...