How to reference a .css file on a razor view?
For CSS that are reused among the entire site I define them in the <head>
section of the _Layout
:
<head>
<link href="@Url.Content("~/Styles/main.css")" rel="stylesheet" type="text/css" />
@RenderSection("Styles", false)
</head>
and if I need some view specific styles I define the Styles
section in each view:
@section Styles {
<link href="@Url.Content("~/Styles/view_specific_style.css")" rel="stylesheet" type="text/css" />
}
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.
How to include CSS styles inline in Razor view?
I guess you will need to have a custom helper for that. On top of my head, there is no such a method to render the css path including the absolute path of the website.
e.g. http:www.example.com/css/EmailStyles.css
Add css file to cshtml file?
Try this below at end of the body tag
@section Scripts
{
<link href="~/Content/yourcssname.css" rel="stylesheet" />
}
ASP.Net MVC: How to use razor variable in CSS file
By default, MVC does not support writing server side codes in external CSS file and external Javascript files.
For your case to work, you can add an internal stylesheet in your view file itself, it will work.
Eg:
@{
ViewBag.Title = "Title";
}
<style>
.test {
background: url('@Model.LogoUrl') no-repeat scroll 0 0 transparent;
}
</style>
How to use @RenderPage to include CSS content inline - MVC Razor
So, it turns out that @RenderPage
is not what I was looking for...
what I needed is this:
@Html.Raw(File.ReadAllText(Server.MapPath("~/CSS/_ExportStyles.min.css")))
Related Topics
How to Overwrite Angular 2 Material Styles
Bootstrap: Align Input with Button
CSS Get Height of Screen Resolution
Why "Overflow: Hidden" Clears a Float
Blocky Gradient Effect in CSS3
Zoom Control and Streetview Not Showing on My Google Map
Changing an Input's HTML5 Placeholder Color with CSS Does Not Work on Chrome
How to Apply a CSS Class to HTML.Actionlink in ASP.NET MVC
Why on Safari the Transform Translate Doesn't Work Correctly
Why Doesn't Min() (Or Max()) Work with Unitless 0
How to Remove Extra Margin Space Generated by Inline Blocks
What Is This CSS Selector? [Class*="Span"]
Google Warning: Resource Interpreted as Font But Transferred with Mime Type Application/Octet-Stream
How to Align Nav Items to the Right in Bootstrap 5
Parent Element Backdrop-Filter Does Not Apply for Its Child
Bootstrap 3: Pull-Right for Col-Lg Only