MVC: The easiest way to link to an app url?

I have a page where I want to include a "Home" link that will lead me to my application base URL. So far, the simplest way I have been able to achieve is the following line of Razor code, but this is not very good, and I am not very sure about this:

@Html.RouteLink(MyResources.HomeLinkLabel, new { controller = "" }) 

Please note that if I do not include controller = "" , then the hyperlink that it creates takes me to the current page, and not to my base URL.

I feel like I'm missing something obvious ... What is the right way to do this?

+4
source share
3 answers

You can use the following code to get the root URL

 Url.Content("~/"); 

The server-side syntax ~/ refers to the root of your application (this means that it will be taken into account if your application is registered in the virtual path in IIS).

+9
source

If you want to go to a specific action, you can simply specify the name of the controller and the action you want to perform:

 @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> 
+3
source

You can also just write plain ol 'HTML:

 <a href="/">@MyResources.HomeLinkLabel</a> 
0
source

Source: https://habr.com/ru/post/1416404/


All Articles