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

1
  • 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. Commented Jul 2, 2013 at 14:04

5 Answers 5

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")
    };
Sign up to request clarification or add additional context in comments.

2 Comments

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")
@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.
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;
}

Comments

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....

Comments

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;
    }

Comments

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");
 }

Comments

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.