1

When I return base or custom object from SxcApiController everything work OK, but If I try to return:

"HttpResponseMessage", Something like:

return Request.CreateResponse(HttpStatusCode.BadRequest, new {  Message = "ERROR : " + ex.Message } );

I get error:

'System.Net.Http.HttpResponseMessage' 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'.",

How can I fix this? How to add additional reference to api controller?

I already add : "using System.Net.Http;" in api controller but this is not the problem...

== Added =============================

I found this code inside 2sxc.csproj file:

<Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />

And there is Version=2.0.0.0

Can this be a problem?

== Complete controller =================================

using System;
using System.Net.Http;
using System.Web.Http;
using DotNetNuke.Data;
using DotNetNuke.Web.Api;
using DotNetNuke.Security;
using ToSic.SexyContent.WebApi;

public class InstallController : SxcApiController
{
    [HttpGet]
    [AllowAnonymous]
    //[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Host)]
    //[ValidateAntiForgeryToken]
    public object DbTablesCreate()
    {
        var result = new {  P1 = "test1", P2 = "test2" } ;
        try
        {
            // Some code I want to protect...
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest, result);
            //return new {  Message = "Returned error with status code OK" };
        }

        return Request.CreateResponse(HttpStatusCode.OK, result);
        //return new {  Message = "Returned ok with status code OK" };
    }
}

Complete result : 500 Internal Server Error

{ "Message": "2sxc Api Controller Finder: Error while selecting / compiling a controller for the request. Pls check the event-log and the code. See the inner exception for more details.", "ExceptionMessage": "c:\websites\dev.lea-d.si\Portals\0\2sxc\nnApp\api\InstallController.cs(23): 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'.", "ExceptionType": "System.Web.HttpCompileException", "StackTrace": " at System.Web.Compilation.AssemblyBuilder.Compile() at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetCompiledAssembly(String virtualPath) at ToSic.SexyContent.WebApi.AppApiControllerSelector.SelectController(HttpRequestMessage request) in C:\Projects\2SexyContent\Web\DesktopModules\ToSIC_SexyContent\Sxc WebApi\AppApiControllerSelector.cs:line 77" }

============== Solution1 ================

I copy the

System.Net.Http.dll

to Bin folder of site and then also add:

using System.Net;
using System.Net.Http;

Now stuf work

Nkosi
  • 191,971
  • 29
  • 311
  • 378
Jernej Pirc
  • 486
  • 1
  • 4
  • 12
  • The question in its current state is unclear as it is incomplete. Read [ask] and then provide a [mcve] that can be used to better understand and reproduce your problem. – Nkosi Nov 27 '17 at 10:14
  • If I try to use Request.CreateResponse inside SxcApiController I get error described in question. If I return string, bool or some anonimuos then all work OK, but then is also always status 200 in response, and I like to return NotFound or other statuses with CreateResponse – Jernej Pirc Nov 27 '17 at 10:24
  • Show more of the controller action. – Nkosi Nov 27 '17 at 10:34
  • @Nkosi I added complete controller code and complete error response – Jernej Pirc Nov 27 '17 at 14:53
  • Just realized you got it working by adding the dll. – Nkosi Nov 27 '17 at 18:07

2 Answers2

1

I copy the file:

System.Net.Http.dll

to Bin folder of the site and then also add:

using System.Net;
using System.Net.Http;

Now stuff work

Jernej Pirc
  • 486
  • 1
  • 4
  • 12
0

As the error message stated, you needed to add the requested dll to the site. The framework searches the bin directory of the site for the necessary references in order to operate correctly.

Also assuming that the controller is derived from ApiController you can try the following using a different syntax

[HttpGet]
[AllowAnonymous]
//[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Host)]
//[ValidateAntiForgeryToken]
public IHttpActionResult DbTablesCreate() {
    var result = new {  P1 = "test1", P2 = "test2" } ;
    try {
        //...code removed for brevity
    } catch (Exception ex) {
        return Content(HttpStatusCode.BadRequest, result);
    }
    return OK(result);
}
Nkosi
  • 191,971
  • 29
  • 311
  • 378