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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:53:58+00:00 2026-06-01T23:53:58+00:00

When using MVC, returning adhoc Json was easy. return Json(new { Message = Hello});

  • 0

When using MVC, returning adhoc Json was easy.

return Json(new { Message = "Hello"});

I’m looking for this functionality with the new Web API.

public HttpResponseMessage<object> Test()
{    
   return new HttpResponseMessage<object>(new { Message = "Hello" }, HttpStatusCode.OK);
}

This throws an exception as the DataContractJsonSerializer can’t handle anonymous types.

I have replaced this with this JsonNetFormatter based on Json.Net.
This works if I use

 public object Test()
 {
    return new { Message = "Hello" };
 }

but I don’t see the point of using Web API if I’m not returning HttpResponseMessage, I would be better off sticking with vanilla MVC. If I try and use:

public HttpResponseMessage<object> Test()
{
   return new HttpResponseMessage<object>(new { Message = "Hello" }, HttpStatusCode.OK);
}

It serializes the whole HttpResponseMessage.

Can anyone guide me to a solution where I can return anonymous types within a HttpResponseMessage?

  • 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-01T23:54:00+00:00Added an answer on June 1, 2026 at 11:54 pm

    This doesn’t work in the Beta release, but it does in the latest bits (built from http://aspnetwebstack.codeplex.com), so it will likely be the way for RC. You can do

    public HttpResponseMessage Get()
    {
        return this.Request.CreateResponse(
            HttpStatusCode.OK,
            new { Message = "Hello", Value = 123 });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Asp.Net-Mvc, I have this method in my controller: [AcceptVerbs(HttpVerbs.Post)] public ActionResult LinkAccount(string
I have a web app that I am building using asp mvc with nhiberante
I am using Spring MVC with Controllers, my question is how do I return
In my Ajax request (using jQuery ) I am returning a JSON response. So
I'm using this helper method to turn my PartialViewResult into string and returning it
I'm looking at using ASP.Net MVC as a platform for a REST based Service.
This is nuts. I have an ASP.NET MVC application using Windows authentication that, amongst
I am using a controller implementation that is extending the Spring MVC AbstractWizardFormController This
I have a problem while returning jsp from controller using ajax+spring mvc. I want
I am attempting to write truly RESTful web services over HTTP using ASP.NET MVC

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.