HttpResponseMessage not working in Web Api (.NET 4.5)

I read http://www.asp.net/web-api/overview/security/working-with-ssl-in-web-api and tried to use the code from that page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;

public class RequireHttpsAttribute : AuthorizationFilterAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        if (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)
        {
            actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)
            {
                ReasonPhrase = "HTTPS Required"
            };
        }
        else
        {
            base.OnAuthorization(actionContext);
        }
    }
}

When I build I don't get an error, but runtime gives this error:

CS0012: The type 'System.Net.Http.HttpRequestMessage' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I have got a reference to System.Net.Http in my references folder of my project. If I look at it's properties, it says Version 4.0.0.0 and Runtime Version 4.0.30319. My project properties says target framework is .NET 4.5.

My IntelliSense in VS2013 Express also doesn't want to pick up anything to do with HttpResponseMessage or HttpRequestMessage.

I've tried removing the reference and re-adding it, but to no avail.

Any help would be tremendously appreciated.

In your web.config try adding:

<configuration>
    <system.web>
         <compilation>
             <assemblies>
                <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
             </assemblies>
          </compilation>
    </system.web>
</configuration>

c#, In your web.config try adding: <configuration> <system.web> <compilation> <​assemblies> <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral,​  Stack Overflow Public questions and answers Teams Private questions and answers for your team Enterprise Private self-hosted questions and answers for your enterprise

Well, I might be late, but just in case someone else faced this problem.

First of all, you need to find a string:

<compilation debug="true" targetFramework="4.5"/>

And make it not self-closing tag like that:

<compilation debug="true" targetFramework="4.5">
</compilation>

Next, add assemblies tag inside with assembly information you mansioned before, so it looks like this:

<compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </assemblies>
</compilation>

And rebuild your solution. Take a look at this link.

Action Results in Web API 2, how ASP.NET Web API converts the return value from a controller action into an HTTP response message in ASP.NET 4.x. Copy. HTTP/1.1 204 No Content Server: Microsoft-IIS/8.0 Date: Mon, 27 Jan 2014 02:13:26 GMT  Web Api Get() route not working when return type is HttpResponseMessage. This is causing me a bit of a headache. Ultimately, I'm trying to return an image but I've been experimenting and simplified this to using a string.

I had tried this highly suggested solution (I found this as accepted answer in many similar questions here in SO)

In your Web.Config write this and rebuild the solution

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
       <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </assemblies>
    </compilation>
</system.web>

But unfortunately, I had no luck with this. At last, I found a solution which worked for me and saved my day :)

  1. Right-click the References folder > Add Reference...
  2. Expand Assemblies on the left side of the window and select Framework.
  3. Scroll to and select System.Net.Http in the list of assemblies. Make sure the box next to System.Net.Http is checked, then click OK.
  4. Rebuild the project.

HttpResponseMessage Class (System.Net.Http), NET 4.5. This means that versions later than 4.0.30506 of the ASP.NET Web API The type or namespace name 'HttpResponseMessage' could not be found (are NET Web API NuGet package may cause security issues in your application  When we talk about response messages in Web API, we simply represent the receiving of some information from the method(s) that we created in Web API so that we can know what kind of response we will get, such as - success or failure. We can use HTTPResponseMessage to return the data as well as some user friendly messages.

Try adding assembly binding in config file

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" />
      <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime

How to: Install Web API NuGet Package on .NET Framework 4.0, It will send 204 "No Content" status code as a response when you send HTTP The advantage of sending HttpResponseMessage from an action method is that you NET 4.5). An action method in Web API 2 can return an implementation of​  Tom Jarvis October 5, 2017 ASP.NET 4.5 Hosting A HttpResponseMessage allows us to work with the HTTP protocol (for example, with the headers property) and unifies our return type. In simple words an HttpResponseMessage is a way of returning a message/data from your action.

Had the same problem, I just reinstalled 'System.Net.Http' Nuget Package and it worked.

create Web API response, .NET 4.5 includes HttpClient class to overcome the limitation of WebClient. Let's see how to consume Web API using HttpClient in the console application. IsValid) return BadRequest("Not a valid data"); using (var ctx = new completes, we get the result from Task using Task.result which is HttpResponseMessage. Now  This also used to work, but I started developing other parts to my API and just came back to building controllers so I do not know what happened. Any thoughts? c# asp.net-mvc-4 visual-studio-2012 asp.net-web-api

Consume Web API in .NET using HttpClient, NET 4.x app, or the organization you work for hasn't approved the use of . You actually want your API to use JSON, not XML, so let's fix that. public HttpResponseMessage Post([FromBody]CustomListItem model) { if(string. I have a Web API project and right my methods always returns HttpResponseMessage. return Request.CreateResponse(HttpStatusCode.OK,"File was processed."); return Request.CreateResponse(HttpStatusCode.NoContent, "The file has no content or rows to process."); I would like to know how can I return to my HTML5 client a better encapsulated respose

Build a REST API with ASP.NET Web API, NET Web API Config; Apply [RequireHttps] attribute on API controller actionContext.Response = new HttpResponseMessage (System.Net. No. ASP.NET MVC 4.5 sees that you're returning Task<HttpResponseMessage> and not HttpResponseMessage, so it will not send the response until your Task is completed (at the end of your async Post method).

How To Enable HTTPS In ASP.NET Web API, If it isn't installed, you can download it from http://www.asp.net/web-api. NET MVC/Web API template to quickly get up and running, I'll take you through the HttpResponseMessage allows you to customize the response in great detail. NET 4.5. In the meantime, you can start using Web API today in its beta form with its  A HttpResponseMessage allows us to work with the HTTP protocol (for example, with the headers property) and unifies our return type. In simple words an HttpResponseMessage is a way of returning a message/data from your action. The following is a quick glimpse of that: // GetEmployee action. public HttpResponseMessage GetEmployee (int id)

Comments
  • Don't forget the compilation element in between system.web and assemblies.
  • This did the trick for me. It compiled with no issue, but intelliSense showed it as an error in the razor view engine until I restarted Visual Studio.