1

I'm currently trying to unit test an ApiController I've created. What I'm stumbling over is that for some reason I can't "use" System.Web.Http. In my main project though I can use the using without any problem (and thus use IHttpActionResult).

Now what I'm not getting is why is it not possible in the unit test Project? Am I overlooking here something or do I need to do something differently?

Francesco B.
  • 2,729
  • 4
  • 25
  • 37
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • Sorry for asking something trivial, but did you reference System.Web in your project? – Francesco B. Mar 23 '18 at 10:49
  • @FrancescoB. I know ist mostly the trivial things thus that was the first Thing I did. I even went so far that I included most Sub references then (also.entity,... ) which the main application had, but no visible effect (sadly). – Thomas Mar 23 '18 at 10:50

1 Answers1

4

As suggested here, System.Web.Http can be installed via NuGet

Get-Project MyTestProject | Install-Package Microsoft.AspNet.WebApi.Core

I tested it in my Unit Test Project and it worked, actually downloading it. System.Web.Http.

After all, the System.Web namespace doesn't seem to contain any Http member.

Anyway, since you said in the comments section that you already had System.Web.Http in your references, off the top of my head I'd say that maybe some dependencies were missing before installing the NuGet package.

WebApi.Core depends on

  • WebApi.Client (see link), which in turn depends on
  • Newtonsoft.Json (see link)

A very, very similar question was also asked here.

Francesco B.
  • 2,729
  • 4
  • 25
  • 37
  • 1
    Yeah that worked. Although have to ask....WHY? (the System.web.http dll was even in the packages from the start but it still only worked after doíng the install). Is there any known reason as to why this is the case? – Thomas Mar 23 '18 at 11:00
  • My guess is you were missing some other dependency, I edited my answer to reflect that. – Francesco B. Mar 23 '18 at 11:10
  • 1
    ah tnx that would be a possibility (newtonsoft I know I had already as I used JObjects webapi.Client I'm not sure about if the Project had included). tnx! – Thomas Mar 23 '18 at 11:37