Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.MapPath VS. @Href("~/SomeFile.cshtml")

I have followed the tutorials over at MSDN and they all (from what I've seen) seem to use @Href() for URL's. E.g.

<a href="@Href("~/")">Some link</a> but I remember reading on here months ago that it is more secure to use Server.MapPath(), E.g.

<a href="@Server.MapPath("~/")">Some link</a> since it converts it to a full path, meaning that you cannot edit the underlying code to change where the form gets submitted to (or something like that). Is this true?

Should I use Href() or Server.MapPath()? Which one is better, and why?

like image 854
jay_t55 Avatar asked Jan 29 '26 11:01

jay_t55


1 Answers

More secure? I don't see a reason why - but I would use neither in Razor. Here's why:

As far as I remember, the Href function is from the ASP.NET 1.0 times. When doing WebForms (!) code you can just paste the ~-URL <a href="~/" runat="server">Some link</a>.

However, if you're doing ASP.NET MVC (which I assume you're doing since you're using Razor) you should rather use Url.Content() which fits with Url.Action() name-wise.

like image 141
Lucero Avatar answered Jan 31 '26 00:01

Lucero