Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:01:05+00:00 2026-06-10T04:01:05+00:00

I am having an issue returning css from a web api controller. The code

  • 0

I am having an issue returning css from a web api controller. The code takes a request for a css file and returns it after reading it from the database.

The problem is that the web api code seems to be serializing the response and returning that instead of the css itself.

Here you can see a link tag that the browser is sending to the server which should return css. You can also see that the response looks like a serialization of my css instead of just the css string.

enter image description here

My request and response headers:

enter image description here

My controller looks like this:

public HttpResponseMessage Get(string fileName, string siteId, int id)
{
    var fileData = ReadSomeCssFromTheDatabase();

    var result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(fileData);
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/css");

    result.Headers.CacheControl = new CacheControlHeaderValue();
    result.Headers.CacheControl.MaxAge = TimeSpan.FromHours(0);
    result.Headers.CacheControl.MustRevalidate = true;

    return result;
}

There is a “text/css” formatter installed that is being created but not being hit for some reason.

public class CssFormatter : MediaTypeFormatter
{
    public CssFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/css"));
    }

    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
    {
        var taskCompletionSource = new TaskCompletionSource<object>();
        try
        {
            var memoryStream = new MemoryStream();
            readStream.CopyTo(memoryStream);
            var s = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            taskCompletionSource.SetResult(s);
        }
        catch (Exception e)
        {
            taskCompletionSource.SetException(e);
        }
        return taskCompletionSource.Task;
    }

    public override bool CanReadType(Type type)
    {
        return type == typeof(string);
    }

    public override bool CanWriteType(Type type)
    {
        return false;
    }
}

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:01:06+00:00Added an answer on June 10, 2026 at 4:01 am
    • Your formatter would not be hit because you are not going through content negotiation process (as you are returning HttpResponseMessage in your action…you could use Request.CreateResponse<> to make conneg process run)

    • You are trying to ‘write’ the css content right?…but i see that CanWriteType is returning ‘false’ and also you seem to be overriding ReadFromStreamAsync instead of WriteToStreamAsync?

    An example of how you could do(from what i understood about the above scenario):

    public class DownloadFileInfo
    {
        public string FileName { get; set; }
        public string SiteId { get; set; }
        public int Id { get; set; }
    
    }
    
    public HttpResponseMessage Get([FromUri]DownloadFileInfo info)
        {
            // validate the input
    
            //Request.CreateResponse<> would run content negotiation and get the appropriate formatter
            //if you are asking for text/css in Accept header OR if your uri ends with .css extension, you should see your css formatter getting picked up.
            HttpResponseMessage response = Request.CreateResponse<DownloadFileInfo>(HttpStatusCode.OK, info);
    
            response.Headers.CacheControl = new CacheControlHeaderValue();
            response.Headers.CacheControl.MaxAge = TimeSpan.FromHours(0);
            response.Headers.CacheControl.MustRevalidate = true;
    
            return response;
        }
    
    public class CssFormatter : MediaTypeFormatter
    {
        public CssFormatter()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/css"));
        }
    
        public override bool CanReadType(Type type)
        {
            return false;
        }
    
        public override bool CanWriteType(Type type)
        {
            return type == typeof(DownloadFileInfo);
        }
    
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            //use the 'value' having DownloadFileInfo object to get the details from the database.
            // Fead from database and if you can get it as a Stream, then you just need to copy it to the 'writeStream'
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue with NSDictionary returning null for an NSString even though
Hi guys I am having issue I have this query: SELECT * FROM useraccount
Ok, having an issue withe the return value of a function always returning undefined
I'm having an issue using Stripes validation method. It works but rather than returning
Issue I'm having is returning some data in a sidebar depending on which category
I'm having a big issue with a static HTTPS connection method. Every second request
I'm having an issue with Ruby 1.8.7 strftime where the %z is returning the
Having an issue with getting a static method from a parent object. Examine the
I'm having an issue where the property IsInDesignMode is not returning the expected value
I am having an issue with the following code. It uses the Mini-XML library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.