What's the difference between @ Url.Content ("~ / css / bootstrap.css") and only "/css/bootstrap.css"

4

I'm learning to program an application based on MVC4 and Bootstrap. Right at the beginning of the project I came across a puzzle:

In _Layout.cshtml I had <link href="css/bootstrap.css" rel="stylesheet"> , Bootstrap worked fine in view ( Home/BootSchool ) and in the other did not format HTML (% ). After some research I solved my problem using Student/AllStudents .

Will it have something to do with the redirect? In the source code of the page, the link is the same in both modes and clicking on it is correctly redirected to the Bootstrap CSS.

    
asked by anonymous 12.12.2016 / 19:09

2 answers

5

This:

<link href="css/bootstrap.css" rel="stylesheet">

It does not look like this, as it may seem:

<link href="@Url.Content("~/css/bootstrap.css")" rel="stylesheet">

In the first mode, the file path will be relative. That is:

/Student/AllStudents/css/bootstrap.css

In the second mode, you are clearly saying that the CSS font should be looked for starting at the root, because you asked for the UrlHelper solve this for you (using the tilde, which indicates the root path of the route). That is:

/css/bootstrap.css

It is considered bad practice to refer to a file without passing UrlHelper just because this relative route problem can occur.

    
13.12.2016 / 04:23
3

They changed this in version 4. You can also use direct ~/css/file.css .

    
13.12.2016 / 02:55