How can I use Razor in CSS files?
I'm using Razor View Engine for some time and I was curious about using it on style sheets. I can use Razor in <style>
blocks of .cshtml files but I was wondering if I can use it in external .css files also (would like to have a .cscss format). So I googled it and found two things:
The first one is LESS: "The dynamic stylesheet language". It seems easy-to-use and powerful with all the features but I need Razor-C#, really.
The second is Dynamic CSS Using Razor Engine, a CodeProject article which is more like what I want but it has no caching or pre-compiling support (by "no support" I meant the writer didn't mention these aspects). I also would like to have some syntax highlighting in Visual Studio but this is secondary.
So, how can I write Razor in CSS files with minimum performance cost and preferably with syntax highlighting?
As a side note:
I found a project called RazorJS. It's like the Javascript version of the same thing I want for CSS with its caching support. I'm mentioning this just to clarify my needs. I don't need to use Razor in Javascript currently but I guess if I make it out with CSS, doing the same thing with Javascript wouldn't be too hard.
Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic.
All Razor files end with . cshtml. Most Razor files are intended to be browsable and contain a mixture of client-side and server-side code, which, when processed, results in HTML being sent to the browser.
It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.
color = "red"; you can apply the style change dynamically. Below is a function that turns an element's colour to red when you pass it the element's id . You could also use setAttribute(key, value) to set a style on an element. For example, you could set the colour of an element to red by calling element.
You could create a custom view engine:
public class CSSViewEngine : RazorViewEngine
{
public CSSViewEngine()
{
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cscss",
"~/Views/Shared/{0}.cscss"
};
FileExtensions = new[] { "cscss" };
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
controllerContext.HttpContext.Response.ContentType = "text/css";
return base.CreateView(controllerContext, viewPath, masterPath);
}
}
and also register it with a custom extension in Application_Start
:
ViewEngines.Engines.Add(new CSSViewEngine());
RazorCodeLanguage.Languages.Add("cscss", new CSharpRazorCodeLanguage());
WebPageHttpHandler.RegisterExtension("cscss");
and inside web.config associate the extension with a build provider:
<compilation debug="true" targetFramework="4.0">
<assemblies>
...
</assemblies>
<buildProviders>
<add extension=".cscss" type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</buildProviders>
</compilation>
[note, if you get an assembly binding error you might need to change the version number in the extension type to match your version of the Razor engine. You can check which version you are using by looking at the properties of your reference to the System.Web.WebPages.Razor assembly in your project]
And the last step is to have some controller:
public class StylesController : Controller
{
public ActionResult Foo()
{
var model = new MyViewModel
{
Date = DateTime.Now
};
return View(model);
}
}
and a corresponding view: (~/Views/Styles/Foo.cscss
):
@model AppName.Models.MyViewModel
/** This file was generated on @Model.Date **/
body {
background-color: Red;
}
which could now be included as a style in the Layout:
<link href="@Url.Action("Foo", "Styles")" rel="stylesheet" type="text/css" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With