Asked by:
Is it possible to use the <a> tag helper to generate a hyperlink to a Razor Component which has a @page directive?

Question
-
User1997578846 posted
Is it possible to use the <a> tag helper to generate a hyperlink to a Razor Component which has a @page directive?
This is in a mostly Razor-Pages project which uses Server-Side Blazor and has routing for both Razor pages and Razor 'Component-Pages'.
The /pages folder has a mix of `.cshtml` files and `.razor` files.
Routing is currently working properly for both Razor Pages and Razor Components with @page directives.Thanks!
Tuesday, March 10, 2020 11:05 PM
All replies
-
User1120430333 posted
Why do you need an <a> tag helper? You can't make an <a> using href,?
the path is.
Pages/Author Pages/Payroll Pages/Article, etc., etc.
@page @model IndexModel @{ ViewData["Title"] = "Author Page"; } <h1>Authors</h1> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Authors</title> </head> <body> <a asp-page="/Author/Create">Create</a> <br /><br /> <table border="1" cellpadding="10"> <tr> <th>AuthorID</th> <th>First Name</th> <th>Last Name</th> <th colspan="4">Actions</th> </tr> @foreach (var item in Model.AuthorVM.Authors) { <tr> <td>@item.AuthorID</td> <td>@item.FirstName</td> <td>@item.LastName</td> <td> <a href="/Author/Edit?id=@item.AuthorID">Edit</a> </td> <td> <a href="/Author/Detail?id=@item.AuthorID">Detail</a> </td> <td> <a href="/Author/Index?id=@item.AuthorID&handler=Delete" onclick="return confirm('Are you sure, you want to delete this author?')">Delete</a> </td> </tr> } </table> </body> </html>
Wednesday, March 11, 2020 2:23 AM -
User-854763662 posted
Hi zxcvbdnm ,
Not clear about what you want. Did you want to click the <a> tag helper in Razor Component and then call the handler of the razor page ?
What's the code you have tried ? And what's the actual result and the expected result ?
Could you share the relevant code so that we can help you better ?
Best Regards,
Sherry
Wednesday, March 11, 2020 7:11 AM -
User1997578846 posted
Pages/DuckPage.razor
@page "/Ducks" <h3>DuckView</h3> @code { }
In Index.cshtml
<a asp-page="/Ducks">Ducks</a>
expected to produce
<a href="/Ducks">Ducks</a>
produces
<a href>Ducks</a>
I'm now realizing being able to do this isn't as important as I thought it might be (kinda)...
What I really want is a better way to link between Razor Pages / Razor Components (w/@page directive).
Ultimately, what I'm looking for is a way to refactor the names of pages (and maybe even query params) without needing to manually change every `href` smattered around my application.
What is the best solution here?
Even ignoring Razor Components and just using Razor Pages I can't see one. When I used MVC I could link to the nameof() the action and that was a bit better I think, but not a complete solution still.I suppose my original question might have been a bit off.
Thanks for the help!Wednesday, March 11, 2020 3:33 PM -
User1120430333 posted
<a href="/Ducks">Ducks</a>
If you expected the above, then why don't you just use it? I found out that the <a> helper tag doesn't always create the needed HTML pathing. So I just didn't use the helper tag, and created what I needed.
Wednesday, March 11, 2020 6:17 PM -
User-854763662 posted
Hi zxcvbdnm ,
As bruce suggested , using directly "href" would be a better choice.
asp-page represnts the razor page to link to and it can not be used for raozr component . Refer to the below:
https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper
Best Regards,
Sherry
Friday, March 13, 2020 10:15 AM