13

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.

7 Answers 7

15

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
It's the solution for 'Visual Studio Mac'.
11

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>

1 Comment

Don't forget the compilation element in between system.web and assemblies.
1

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.

Comments

1

I did all of the above mentioned solutions for my problem of at:

actionContext.Response = new HttpResponseMessage(statusCode: HttpStatusCode.UpgradeRequired);

Where .Response and the HttpResponseMessage & HttpStatusCode would error out with

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 removed and re-added the reference to System.Net.Http, added the entry in the web.config file and also installed it from Nuget package manager using the command:

PM> Install-Package System.Net.Http

and yet it did not work. Ultimately I closed and opened the solutions with all of the above fixes in, and it started working.

Comments

0

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

Comments

0

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

Comments

0

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

Right-click the References folder > Add Reference... Expand Assemblies on the left side of the window and select Framework. 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. Rebuild the project.

This one is working fine. Go Head

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.