CSS includes MVC

So, I know that there are different ways to do it, however I would like to know the “right” way to include certain CSS dynamically based on the page I'm on. I could include them in the site’s master, however, I’m wondering if I can just include them ONLY when I need it, either by evaluating the URL, or by passing a value through the controller to display the flag, or simply by including it in the content (outside headers) ... I would like it to be clean and associate them with all my site host, but I would like to evaluate the page I work on before incorporating this CSS ..... thoughts ??

+3
source share
4 answers

Whatever will look something like this:

<% if( someCriteria ) { %>
   <stylesheet type="text/css" href="mypath" />
<%} %>

You can wrap it in an assistant or something else, but I don't think there might be a better practice or a “cleaner” way to make something so simple.

"I could include them in the site’s master, but I wonder if I can just include them ONLY when I need it."

Another way to look at this is that CSS files are cached by the browser, so you can enable it once and do it. Your visitors may have a slightly longer initial loading time, but if you keep your CSS files small, it will be barely noticeable. Very few advantages at work, making it dynamic.

+1
source

, , , -, ContentPlaceHolder <head> . CSS CSS, , . ASP.NET MVC.

+1

, , , . , , .

I would also vote to get an Html helper to handle this, so your developers do not need to worry about where the stylesheet is loaded.,.

0
source

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


All Articles