skip to Main Content

I use the following in MVC to create a link to my home page in my application:

<a href="~/">home</a>

Unfortunately, when this is run under an app, this will create a link like

<a href="/app-name/">home</a>

Is there a way to create this link to the app-name without the trailing slash (or without just hardcoding the it)?

Our site is canonicalised so that any urls with a trailing slash end up being redirected to their non trailing slash version and because this link has a trailing slash on, it is causing unnecessary redirect hops, which is penalising our seo ratings

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to the comment from Lajos Arpad, it got me of thinking into another of making up the url so I thought if I use the VirtualPathUtility.ToAbsolute I may be able to then substring the result.

    But using the following meant I didn't need to substring to get the url I needed:

    <a href="@VirtualPathUtility.ToAbsolute("~")">Home</a>
    

  2. The Html.ActionLink helper should generate a link that conforms to your requirements:

    @Html.ActionLink("home", "Index", "Home")
    

    Assuming your homepage is called “Index” and rendered by the “HomeController”.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search