Search code examples
c#asp.net-web-apidotnetnuke2sxc

2sxc - HttpResponseMessage reference missing in Custom api controller


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


Solution

  • 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