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 642469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:10:30+00:00 2026-05-13T21:10:30+00:00

I put together the following method, which calls my repository to get an image

  • 0

I put together the following method, which calls my repository to get an image (returned as Linq.Binary). It then writes the byte array to the OutputStream and sets the content type:

    public EmptyResult GetImage(int id)
    {
        Byte[] imageBytes = _repository.GetImage(id).ToArray();

        Response.OutputStream.Write( imageBytes, 0, imageBytes.Length);
        Response.ContentType = "image/png";

        return new EmptyResult();
    }

This is one of those times where I wrote a block of code, it worked first try (always scary), and now I’m trying to think through where it will break.

My requirements are just to return an image that I’m getting from the database, no text.

What I’m doing above allows me to avoid the use of converting to a Bitmap (or other Image type), I don’t need to have references to System.Drawing, I don’t need to write a custom ActionResult and I don’t need to worry about disposing resources.

I’d have to think a little further through, but I think this also opens the door to leveraging the MVC OutputCache without much pain.

I see people writing custom ActionResults and am trying to figure out the benefit. I mean, I understand the additional flexibility and ability to handle multiple types, but one solution I found took the Binary.ToArray(), converted to a Bitmap and created an ashx handler to return the image. Is any of that necessary?

Is it bad form to write to the output stream when you’re returning an EmptyResult?

In all current browsers, the above code allows me to display an image with a simple:

 <img id="display-image" 
      src="<%= Url.Action("GetImage", new { id = Model.ImageID }) %>" />

So are their downfalls to this approach?

Thanks for thoughts/comments folks.

Cheers,
-james

  • 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-05-13T21:10:31+00:00Added an answer on May 13, 2026 at 9:10 pm

    Is it bad form to write to the output stream when you’re returning an EmptyResult?

    Yes, but there are plenty of more appropriate result types, such as FileContentResult and FileStreamResult.

    Many people write custom ActionResults because:

    • They started with an early version of MVC, which didn’t have these, or
    • They have highly specific needs not met by the built-in types
    • They don’t want to have to remember to set the mime-type correctly every time, so they build in a type for a specific mime-type (e.g., the built-in JavaScriptResult).
    • Writing to Response makes controllers hard to test. You can write in an ActionResult.ExecuteResult instead and have a testable controller.

    You don’t need a custom result type for an image, but you could write one which wraps up the two lines of code you use to write to Response and set content type.

    E.g., something like:

    public class PngResult : ActionResult {
    
        public Byte[] Data {
            get;
            set;
        }
    
        public override void ExecuteResult(ControllerContext context) {
            if (context == null) {
                throw new ArgumentNullException("context");
            }
    
            if (Data == null)
            {
                throw new InvalidOperationException("Data is null.");
            }
    
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = "image/png";
    
            response.OutputStream.Write(Data);
        }
    
        public PngResult(Byte[] data) 
        {
            if (data == null) {
                throw new ArgumentNullException("data");
            }
            this.Data = data;
        }
    }
    

    Update: On second thought, you should maybe subtype FileContentResult rather than ActionResult and you’d have even less code to write. But you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I put together an image preloader which works fine, but what doesn't work is
I am trying to put together a GWT webapp following this GWT MVP tutorial
I'm trying to put together a Continuous Integration server that will do the following:
I have put together the following code, the problem is that each while loop
I am new to doing Ajax request and have put together the following Pastie
I wonder whether someone can help me please. I've put together this page which
Using various tutorials namely here and here I've managed to put together the following
I'm trying to put together an application which uses YUI's DataTable component but I
I have put together the following mootools script window.addEvent('domready', function() { var shouts =
We need a simple method to put together wireframes. What is the simplest way

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.