Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: Simplest way to create a link to the application base URL?

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?

like image 764
Clafou Avatar asked Dec 16 '22 23:12

Clafou


2 Answers

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

like image 55
marcind Avatar answered Jan 05 '23 00:01

marcind


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>
like image 42
Ivo Avatar answered Jan 04 '23 23:01

Ivo