简体   繁体   English

2sxc-自定义api控制器中缺少HttpResponseMessage参考

[英]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: 当我从SxcApiController返回基础对象或自定义对象时,一切正常,但是如果我尝试返回:

" HttpResponseMessage ", Something like: HttpResponseMessage ”,类似:

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. “ System.Net.Http.HttpResponseMessage”在未引用的程序集中定义。 You must add a reference to assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.", 您必须添加对程序集'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? 如何向api控制器添加其他引用?

I already add : " using System.Net.Http; " in api controller but this is not the problem... 我已经在api控制器中添加了“ using System.Net.Http; ”,但这不是问题。

== Added ============================= ==添加============================

I found this code inside 2sxc.csproj file: 我在2sxc.csproj文件中找到以下代码:

<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 并且有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 完成结果: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 {“ Message”:“ 2sxc Api Controller Finder:为请求选择/编译控制器时出错。请检查事件日志和代码。有关更多详细信息,请参见内部异常。”,“ ExceptionMessage”:“ c:\\ website \\ dev.lea-d.si \\ Portals \\ 0 \\ 2sxc \\ nnApp \\ api \\ InstallController.cs(23):错误CS0012:类型'System.Net.Http.HttpRequestMessage'在未引用的程序集中定义您必须添加对程序集'System.Net.Http,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a'的引用。“,” ExceptionType“:” System.Web.HttpCompileException“,” StackTrace“:” System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath虚拟路径)上System.Web.Comp.ation.BuildProvidersCompiler.PerformBuild()上的System.Web.Compilation.AssemblyBuilder.Compile() ,布尔值noBuild,布尔值allowCrossApp,布尔值allowBuildInPrecompile,布尔值throwIfNotFound,布尔值sureIsUpToDate) 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" } System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext上下文,NoPath ,System.Web.Compilation.BuildManager.GetCompiledAssembly(String virtualPath),ToSic.SexyContent.WebApi.AppApiControllerSelector.SelectController(HttpRequestMessage request)处的System.Web.Compilation.BuildManager处的Boolean allowCrossApp,布尔的allowBuildInPrecompile,布尔的sureIsUpToDate) ToSIC_SexyContent \\ Sxc WebApi \\ AppApiControllerSelector.cs:第77行“}

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

I copy the 我复制

System.Net.Http.dll System.Net.Http.dll

to Bin folder of site and then also add: 到站点的Bin文件夹,然后还添加:

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

Now stuf work 现在工作

As the error message stated, you needed to add the requested dll to the site. 如错误消息所述,您需要将请求的dll添加到站点。 The framework searches the bin directory of the site for the necessary references in order to operate correctly. 框架在站点的bin目录中搜索必要的引用,以使其正常运行。

Also assuming that the controller is derived from ApiController you can try the following using a different syntax 另外,假设控制器是从ApiController派生的, ApiController可以使用其他语法尝试以下操作

[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);
}

I copy the file: 我复制文件:

System.Net.Http.dll System.Net.Http.dll

to Bin folder of the site and then also add: 到站点的Bin文件夹,然后添加:

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

Now stuff work 现在工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM