20

I need a solution that lets me accomplish the following:

  • Returning CSS that is dynamically generated by an action method
  • Choosing CSS file depending on request parameter or cookie
  • Using a tool to combine and compress (minify) CSS

I am currently considering why there is no CssResult in ASP.NET MVC, and whether there might be a reason for its absence. Would creating a custom ActionResult not be the best way to go about this? Is there some other way that I've overlooked to do what I need?

Any other suggestions or hints that might be relevant before I embark on this task will also be appreciated :)

3 Answers 3

33

You need to return a FileResult or ContentResult with a content type of text/css.

For example:

return Content(cssText, "text/css");
return File(cssStream, "text/css");

EDIT: You can make a Css helper method in your controller:

protected ContentResult Css(string cssText) { return Content(cssText, "text/css"); }
protected FileResult Css(Stream cssStream) { return File(cssStream, "text/css"); }
Sign up to request clarification or add additional context in comments.

2 Comments

hi this is similar to what I was looking for, how to call this from the MVC view in the @section { Styles }
@transformer: Just make a normal <link> tag that points to a URL that serves your CSS.
5

No need to create a custom ActionResult type. Since CSS a "just text", you should be fine using ContentResult. Assuming you inherited the Controller class, simply do:

return Content(cssData, "text/css");

Comments

0

I am currently considering why there is no CssResult in ASP.NET MVC, and whether there might be a reason for its absence.

Simply because the team had its hands full and obviously it's some effort to add ActionResults for all cases in life.

Would creating a custom ActionResult not be the best way to go about this?

It would be a correct way to do so. I added RssActionResult and AtomActionResult for my needs. It's also reasonable to add more types, for docs, pdfs, images etc.

Returning CSS that is dynamically generated by an action method

Also keep in mind that a browser would normally cache the css unless it sees some variation in the url. Adding an always incrementing parameter is a usual solution.

<link rel="stylesheet" href="http://site.com/styles.css?v=26">

An extra route parameter for version would probably work as well.

3 Comments

@SLaks: I like it more this way, so that I don't repeat content-type again and again or worse forget it once.
another way for dealing with this type of scenario is to use ETags then you have fine grained control over what should be cached and what shouldn't, if you use this in h conjunction with a cache memory store you can avoid hitting the DB to check. I did this by implementing ETags which are comprised of a hash based off the content of the response, I then store that in a memory store which gets searched every time a request is made. If entry (hash) is there then it returns a 304 not modified. You return your response with an etag header the browser will use If-None-Match header
My method is more complex but it also offers you the ability to utilize the cache and circumvent it only when necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.