I have a page where I want to include a "Home" link which takes me to my application's base URL. So far the simplest way I managed to achieve this is through the following line of Razor code, but it's not pretty and I'm not terribly confident about it:
@Html.RouteLink(MyResources.HomeLinkLabel, new { controller = "" })
Note that if I don't include controller = ""
then the hyperlink it generates takes me to the current page, not my base URL.
I feel I'm missing something obvious... What's the correct way of doing this?
You could use the following code to get the root URL
Url.Content("~/");
The server-side ~/
syntax references the root of your application (meaning it will take into account if your app is registered in a virtual path in IIS).
If you want to go to the an specific action, you could just include the controller name and the action you want to go:
@Html.RouteLink(MyResources.HomeLinkLabel, new { controller = "Home", action = "Index" })
Now, if you want to go to the root, you can just put something like
<a href="@Url.Content("~/")">...</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With