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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:14:47+00:00 2026-06-02T23:14:47+00:00

I am attempting to write truly RESTful web services over HTTP using ASP.NET MVC

  • 0

I am attempting to write truly RESTful web services over HTTP using ASP.NET MVC 4 Web API.

The current challenge I have is to return different return types (entity-body) based upon my status code.

For example, for resource Hammer I have a .NET model class “Hammer”, and a HammerController:

namespace Awesomeness
{
    public class HammerController : ApiController
    {
        public Hammer Get(int id)
        {
        }
...

If the ID doesn’t exist (404) or requires different authorization (401), I can easily shortcut the return and manually set the status code and any other content, which is cool. However, in many non-2xx statuses, I want to return a different entity-body than the Hammer resource representation. I can easily do this manually, but I’d like to take advantage of the ASP.NET MVC 4 Web API automagic serialization and deserialization to/from XML or JSON depending on the request headers.

So my core question is: Can I take advantage of ASP.NET MVC 4 Web API’s automagic serialization while returning differing return types?

Some potential approaches I have thought about are:

  1. Have the controller method return the main resource type, but short-circuit the return with HttpContext.Current.Response and somehow hook into the automagic serialization (preferred).

  2. Have my Model class be more like a C union where it represents this type or that type and let it get serialized as part of the normal return process (and just override status code and any response headers needed). Even if I think through how to do this, I have a feeling it would still end up being very hacky.

Edited Apr 27 2012: I can throw an HttpResponseException like this:

HttpResponseMessage response = new HttpResponseMessage(statusCode);
if (!string.IsNullOrWhiteSpace(text))
{
    response.Content = new StringContent(text);
}
throw new HttpResponseException(response);

…but now I need to figure out how to hook into the auto-magic serialization and set the response.Content to the Accept header negotiated representation.

  • 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-02T23:14:50+00:00Added an answer on June 2, 2026 at 11:14 pm

    I derived the clean answer to this question from an answer I received on this other question.

    If I make my return value HttpResponseMessage, I can return whatever status or content I need to by returning either a new HttpResponseMessage().

    EDIT 5/10/2012: Thanks to input from @DarrelMiller, removed deprecated HttpResponseMessage<T> and replaced with call to HttpRequestMessage.CreateResponse<T>() extension method. NOTE: This extension method is not available in the MVC4/WebAPI beta. You need to build from source or get the nightly build.

    Oversimplified example:

    public HttpResponseMessage Post(MyResource myResource)
    {
        ...
        if (goodStuff)
        {
            return ControllerContext.Request.CreateResponse(HttpStatusCode.Created, myNewResource);
        }
        else if (badStuff)
        {
            return ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest, badRequest);
        }
        else
        {
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to write a TinyURL like clone in ASP.NET MVC as a first
I am attempting to write an ASP.net web service that will be utilized by
I am attempting to write out an Excel file from an ASP.NET web server
I am attempting to write a 'User Control' in WinForms .NET (not ASP.NET). The
I am attempting to write a simple TCP server using Aleph. Everything works fine,
I am attempting to write a value to the HKLM registry using TRegistry component
I am attempting to write to Azure Table Storage without using the TableContext and
I'm attempting to write a simple buffer overflow using C on Mac OS X
I am attempting to write a .NET component. The component will be dropped onto
I'm attempting to write a multi-producer, multi-consumer queue. I'm using G++ 4.6 on Arch

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.