The right way to link to content in MVC

When running my web application on my local computer, I can specify css / scripts / images using:

<link href="/Content/Site.css" rel="stylesheet" type="text/css" />

But when I turned around on my development server, he could not find any of my content. After studying the question, everyone suggests using the method below:

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />
<img src="<%=Url.Content("~/Content/3.png")%>" />

At least now all my content is uploaded and working when I click on the development server. However, now that I have a script server, the Design view in Visual Studio does not load any / etc style. I am wondering if there is an alternative or something I am missing, maybe fix it? Or maybe I will fix everything? Any input is welcome.

+3
source share
3 answers

Your suggestion is a great way to link to content.

For Design View, donโ€™t sacrifice the elegance of your code to get a Design View. Learn to love Code View. Preview in the browser. In my experience, this workflow really has no serious flaws (as soon as you get used to it).

(Aside, I think most developers who like ASP.NET MVC do not use โ€œDesign Viewโ€ in Visual Studio. One of the reasons I love MVC is that it allows me to be picky about markup . Any kind of designer lies outside such thinking.)

+6
source

You can always trick a designer with such cruelty as this:

<% if (false) { %>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<% } %>
<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />

, , F5 , ( ).

+4

Wrong design? This is not a mistake in the code, it is as it is. This is true for ASP.NET MVC and regular ASP.NET

+1
source

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


All Articles