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

  • 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.


    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.

    @model IndexModel
        ViewData["Title"] = "Author Page";
    <!DOCTYPE html>
        <meta name="viewport" content="width=device-width" />
            <a asp-page="/Author/Create">Create</a> 
            <br /><br />
            <table border="1" cellpadding="10">
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th colspan="4">Actions</th>
                @foreach (var item in Model.AuthorVM.Authors)
                            <a href="/Author/Edit?id=@item.AuthorID">Edit</a>
                            <a href="/Author/Detail?id=@item.AuthorID">Detail</a>
                            <a href="/Author/Index?id=@item.AuthorID&handler=Delete"
                               onclick="return confirm('Are you sure, you want to delete this author?')">Delete</a>

    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,


    Wednesday, March 11, 2020 7:11 AM
  • User1997578846 posted


    @page "/Ducks"
    @code {

    In Index.cshtml

    <a asp-page="/Ducks">Ducks</a>

    expected to produce

    <a href="/Ducks">Ducks</a>


    <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:


    Best Regards,


    Friday, March 13, 2020 10:15 AM