How to reference a .css file on a razor view?

后端 未结 6 1471
执念已碎
执念已碎 2020-11-29 15:24

I know how to set .css files on the _Layout.cshtml file, but what about applying a stylesheet on a per-view basis?

My thinking here is that, in _Layout.cshtml, you h

6条回答
  •  无人及你
    2020-11-29 15:57

    For CSS that are reused among the entire site I define them in the section of the _Layout:

    
        
        @RenderSection("Styles", false)
    
    

    and if I need some view specific styles I define the Styles section in each view:

    @section Styles {
        
    }
    

    Edit: It's useful to know that the second parameter in @RenderSection, false, means that the section is not required on a view that uses this master page, and the view engine will blissfully ignore the fact that there is no "Styles" section defined in your view. If true, the view won't render and an error will be thrown unless the "Styles" section has been defined.

提交回复
热议问题