Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpResponseMessage可以';不序列化内容_C#_.net_Xml_Xml Serialization_Xmlserializer - Fatal编程技术网

C# HttpResponseMessage可以';不序列化内容

C# HttpResponseMessage可以';不序列化内容,c#,.net,xml,xml-serialization,xmlserializer,C#,.net,Xml,Xml Serialization,Xmlserializer,我有空的.NETRESTAPI,它只接受XML。 如果控制器方法返回我要直接序列化的类,则没有问题。但我想使用响应包装器,以便所有控制器方法返回HttpResponseMessage 所以这是可行的: FooController.cs: public FooResponse PostFoo(FooRequest foo) { var fooVal = fooBll.GetFoo(); return new FooResponse { Foo = fooVal;}; } public H

我有空的.NETRESTAPI,它只接受
XML
。 如果控制器方法返回我要直接序列化的类,则没有问题。但我想使用响应包装器,以便所有控制器方法返回
HttpResponseMessage

所以这是可行的: FooController.cs:

public FooResponse PostFoo(FooRequest foo)
{
  var fooVal = fooBll.GetFoo();
  return new FooResponse { Foo = fooVal;};
}
public HttpResponseMessage PostFoo(FooRequest foo)
{
  var fooVal = fooBll.GetFoo();
  HttpResponseMesage response;
  response = fooBll.GetSuccessResponse(Url, new FooResponse {Foo = foovVal;});
  return response ;
}
但这并不是: FooController.cs:

public FooResponse PostFoo(FooRequest foo)
{
  var fooVal = fooBll.GetFoo();
  return new FooResponse { Foo = fooVal;};
}
public HttpResponseMessage PostFoo(FooRequest foo)
{
  var fooVal = fooBll.GetFoo();
  HttpResponseMesage response;
  response = fooBll.GetSuccessResponse(Url, new FooResponse {Foo = foovVal;});
  return response ;
}
ResponseBell.cs:

public HttpResponseMessage GetSuccessResponse(UrlHelper Url, object obj)
{
    this.requestMsg = HttpContext.Current.Items["MS_HttpRequestMessage"] as HttpRequestMessage;
    this.responseMsg = this.requestMsg.CreateResponse(HttpStatusCode.OK, obj);
    this.responseMsg.Headers.Location = HttpContext.Current.Request.Url;
    return this.responseMsg;
}
FooBLL继承ResponseBLL,这就是为什么我可以调用
FooBLL.GetSuccessResponse

在WebApiConfig.cs中,我有:

config.Formatters.XmlFormatter.UseXmlSerializer = true;
config.Formatters.Remove(config.Formatters.JsonFormatter);
我得到的错误是

类型响应不是预期的。使用xmlclude或 SoapInclude属性指定静态未知的类型

现在我知道在这个问题上已经有很多问题了,相信我,我已经解决了所有这些问题,但都没有成功。我尝试了
[DataContract]、[DataMember]、[Serializable]
[xmlclude]
属性的所有可能组合<代码>[xmlclude(typeof(foooresponse))]在FooBLL类上,代码>[xmlclude(typeof(FooBLL))]在ResponseBLL上,等等

我希望你能帮助我。

是一种通用方法。
T值
参数必须正确键入,不能声明为
对象
,例如:

public HttpResponseMessage GetSuccessResponse<T>(UrlHelper Url, T obj)
{
    this.requestMsg = HttpContext.Current.Items["MS_HttpRequestMessage"] as HttpRequestMessage;
    this.responseMsg = this.requestMsg.CreateResponse(HttpStatusCode.OK, obj);
    this.responseMsg.Headers.Location = HttpContext.Current.Request.Url;
    return this.responseMsg;
}

我们可以看到,只有
type
(作为
typeof(T)
from)传递到
WriteToStreamAsync()
)用于序列化程序构造<代码>对象值不可用。Microsoft可能已经选择使用
value.GetType()
,但是没有。序列化程序随后抛出异常,因为传入的根对象的实际类型(
typeof(Foo)
)与用于构造序列化程序的类型不同(
typeof(object)
)或者是一种已知类型的
对象

使用action进行这一丰富操作会更干净filter@AlekseyL. 谢谢,我会调查的。基本上,我之所以需要这个包装器是因为控制器可以通过这种方式返回不同类型的内容,例如,还有
GetFailResponse
方法,它可以代替内容返回有关错误的信息。没有它,我就无法完成我所看到的,我必须返回
FooResponse
,其他什么都没有。这可以通过异常过滤器或异常处理程序处理谢谢!不能说我完全理解为什么它是这样工作的(xml中的noobie),但事实就是如此!但我想我必须朝着Alleksey L.指出的方向走。同时,我们将使用这个。