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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:38:46+00:00 2026-06-15T00:38:46+00:00

In my attempt to learn and understand WebAPI I think I have learnt that

  • 0

In my attempt to learn and understand WebAPI I think I have learnt that returns from APIController methods are wrapped up into HttpResponseMessage.

You also have the option to create a new HttpResponseMessage and put your List<Product> inside it.

I am interested in testing headers for example and if I pass in a accept header with the below code I want to test for the content type returned in the response.

I know I can always return a HttpResponseMessage because strictly thats what its doing but I was just wondering if there was a way to either cast the response back from a controller method as a HttpResponseMessage without having to create a HttpServer/HttpConfiguration setup as shown here.

        var config = new HttpConfiguration();


        var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products");
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var route = config.Routes.MapHttpRoute(
         name: "DefaultApi",
         routeTemplate: "{controller}"
            );

        var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "GetData" } });
        var controller = new GetDataController();
        controller.ControllerContext = new HttpControllerContext(config, routeData, request);
        controller.Request = request;
        controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

        /************************* HELP!!!***************/
        // If Get returns List<Product> can I cast it as HttpResponseMessage??
        /************************************************/

        var result = controller.Get();

        // Assert
        Assert.Equal("application/json", result.Content.Headers.ContentType.MediaType);
  • 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-15T00:38:48+00:00Added an answer on June 15, 2026 at 12:38 am

    Controller methods are not meant to be called directly like that. You have to send an HTTP request to get an HTTP response. Try using

    HttpResponseMessage response = HttpClient.SendAsync(request).Result;
    

    Although if you are not hosting in IIS you would have to create an HttpSelfHostServer to host the controller inside the process so that it can listen for requests. This will show you how to do that, http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api

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

Sidebar

Related Questions

In attempt to learn Hadoop, I am practicing unsolved programming questions from the book
in an attempt to learn something about ARM assembly, i have written a simple
I am writing a card game in an attempt to learn Silverlight. I have
In an attempt to learn JS OOP I am viewing the source code of
in my attempt to learn a bit faster the use of spring and hibernate
In an attempt to learn to use PInvoke in C#, I'm a little unsure
In an attempt to learn C#, I am trying to develop Picasa Like Application
I'm trying to write a Date class in an attempt to learn C++. I'm
I attempt to use webservice return POCO class generated from entity data model as
In an effort to understand MVC 2 and attempt to get my company to

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.