3

I need to write a Web API method that return result as CSS plain text and not the default XML or JSON, Is there a specific provider that I need to use?

I tried using the ContentResult class (http://msdn.microsoft.com/en-us/library/system.web.mvc.contentresult(v=vs.108).aspx) but no luck.

Thanks

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
Lennie
  • 1,999
  • 4
  • 27
  • 42
  • 1
    That's because Web API and MVC are two completely different frameworks that have been made to look very similar. Unfortunately under the covers they are very different. This one of the reasons I've been editing dozens of posts to remove the use of the term `ASP.NET MVC Web API` because it confuses everyone into thinking they are part of the same framework and that components of the framework are interchangeable. – Darrel Miller Jul 02 '13 at 14:04

5 Answers5

6

You should bypass the content negotiation which means that you should return a new instance of HttpResponseMessage directly and set the content and the content type yourself:

return new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StringContent(".hiddenView { display: none; }", Encoding.UTF8, "text/css")
    };
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • 3
    Just to say I think it is worth using the overload for StringContent to specify the content type as CSS StringContent(css, Encoding.UTF8, "text/css") – Mark Jones Jul 02 '13 at 11:11
  • @MarkJones, great idea (otherwise it would server text/plain which is OK but better to use text/css as you said). I've edited the answer accordingly. – Teoman Soygul Jul 02 '13 at 14:03
0

Using the answers here as inspiration. You should be able to do something as simple as this:

public HttpResponseMessage Get()
{
    string css = @"h1.basic {font-size: 1.3em;padding: 5px;color: #abcdef;background: #123456;border-bottom: 3px solid #123456;margin: 0 0 4px 0;text-align: center;}";
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StringContent(css, Encoding.UTF8, "text/css");
    return response;
}
Community
  • 1
  • 1
Mark Jones
  • 12,156
  • 2
  • 50
  • 62
0

Can you return a HttpResponseMessage, get the file and just return the stream? Something like this seems to work....

    public HttpResponseMessage Get(int id)
    {
        var dir = HttpContext.Current.Server.MapPath("~/content/site.css"); //location of the template file
        var stream = new FileStream(dir, FileMode.Open);
        var response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK, 
                Content = new StreamContent(stream)
            };

        return response;
    }

Although I would add some error checking in there if the file doesn't exist etc....

Dom
  • 330
  • 1
  • 10
0

And just to pile on for fun, here's a version that would work under self-host too assuming you store the .css as an embedded file that sits in the same folder as the controller. Storing it in a file in your solution is nice because you get all the VS intellisense. And I added a bit of caching because chances are this resource isn't going to change much.

  public HttpResponseMessage Get(int id)
    {

        var stream = GetType().Assembly.GetManifestResourceStream(GetType(),"site.css");

        var cacheControlHeader = new CacheControlHeaderValue { MaxAge= new TimeSpan(1,0,0)};

        var response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK, 
                CacheControl = cacheControlHeader,
                Content = new StreamContent(stream, Encoding.UTF8, "text/css" )
            };

        return response;
    }
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
0

For anyone using AspNet Core WebApi you can simply do it like this

 [HttpGet("custom.css")]
 public IActionResult GetCustomCss()
 {
     var customCss = ".my-class { color: #fff }";

     return Content(customCss, "text/css");
 }
Richard Houltz
  • 1,962
  • 1
  • 18
  • 24