Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add individual css file in particular view in mvc4?

Actually I've a master page and I'm adding this master page into another view (Index.cshtml), now I want to add another individual css file into Index.cshtml view.

like image 537
GotiBandhu Huda Avatar asked Sep 19 '12 10:09

GotiBandhu Huda


People also ask

How do I reference a css file on a razor view?

All you have to do is to place a style sheet in the Pages folder alongside the page that it is intended to affect. You just need to follow a specific naming convention, which is the Razor page file name with . css on the end. The bundled file itself is placed in the wwwroot folder when the application is published.

How do I bundle css in MVC?

The following example shows how to combine multiple CSS files into a bundle. public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles. Add(new StyleBundle("~/bundles/css"). Include( "~/Content/bootstrap.


2 Answers

In the head section of your master page (e.g. _Layout.cshtml) add a RenderSection call. Your header section could look like this:

<head>
    <meta charset="utf-8" />
    <meta name="description" content="The content description" />
    <link rel="shortcut icon" href="@Url.Content("~/Content/images/favicon.ico")" />
    <title>@ViewBag.Title</title>
    @RenderSection("css", false)
</head>

Then in your Index.cshtml use the section to add your css link:

@section css {
    <link href="@Url.Content("~/css/style.css")" rel="stylesheet"/>
}

Note that "css" is a unique name for your section and it needs to match. You can also use this section in any view you want. The part you specify within the css section will then be rendered within the head-tag of your html, just where you put the RenderSection placeholder in your _Layout.cshtml.

like image 154
360Airwalk Avatar answered Nov 15 '22 13:11

360Airwalk


Bad practice but you can Just add a html link tag or script tag to the top of the view, modern browsers will still render this. It doesn't always need to be in the head of the master page

like image 20
CR41G14 Avatar answered Nov 15 '22 12:11

CR41G14