Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
Asp.net 获取对WebAPI的调用_Asp.net_Xml_Vb.net_Asp.net Web Api - Fatal编程技术网

Asp.net 获取对WebAPI的调用

Asp.net 获取对WebAPI的调用,asp.net,xml,vb.net,asp.net-web-api,Asp.net,Xml,Vb.net,Asp.net Web Api,我对Web API调用的XML响应有问题。 具体来说,我有一个函数“GetValue”,当我应该基于id或类“Cellulare”或类“Televisiore”以XML格式返回时,调用该函数 问题是,如果我从浏览器发出请求,会出现以下错误: <Message>An error has occurred.</Message> <ExceptionMessage> The 'ObjectContent`1' type failed to serialize the

我对Web API调用的XML响应有问题。 具体来说,我有一个函数“GetValue”,当我应该基于id或类“Cellulare”或类“Televisiore”以XML格式返回时,调用该函数

问题是,如果我从浏览器发出请求,会出现以下错误:

<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
谁能帮我解决这个问题

提前感谢 问候语
Donato

它抛出错误,因为您没有向GetValue函数提供任何返回类型。你把它说出来了

从代码中可以看出,根据您提供给GetValue调用的id,您返回的是不同类型的对象。我不知道您尝试执行的操作的完整上下文,但从我所看到的情况来看,对于不同类型的对象,使用不同的控制器或路由更有意义:

/api/cellulare/<id>
/api/cellulare/
将映射到控制器CellularController

/api/televisore/<id>
/api/televisiore/
将映射到控制器或控制器。如果愿意的话,每个都有自己的Get()、Post()和Delete()方法


希望这有帮助。

我认为你的方法是错误的

您可以返回简单的对象,webapi提供的默认序列化程序可以轻松处理这些对象

返回的对象类型应为
IHttpActionResult
(webapi2)或
HttpResponseMessage

我不会同意@Frank Witte的建议,因为返回对象本身是一种不好的做法。特别是在这里,您可以通过
ihttpackationresult
/
HttpResponseMessage
返回一个通用对象

你应该这样做:

Public Function GetValue(ByVal id As Integer) As IHttpActionResult
    If Id = 1 Then    
        Dim MyTelevisore As New Televisore    
        MyTelevisore.Colore = "grigio"
        MyTelevisore.Marca = "lg"
        Return Ok(MyTelevisore)
    Else            
        Dim MyCellulare As New Cellulare    
        MyCellulare.Colore = "nero"
        MyCellulare.SistemaOperativo = "android"    
        Return Ok(MyCellulare)
    End If    
End Function

您确定这就是原因吗?您可以通过显式设置函数的返回类型来测试它。我认为这种方法是不好的做法。应该返回一些通用对象,例如
ihttpackationresult
/
HttpResponseMessage
。是的,这样更好。我的观点是,您根本没有定义返回类型,而且在XML中也不能很好地序列化。这就是错误信息。好吧,也许你在技术上是对的,但我认为@BlackThunder需要一个更具建设性的答案……非常感谢你的答案。这就是我想要的。我尝试了你建议的例子,效果很好。这个警告出现了:投票需要15个声望!我有6个名声。
Public Function GetValue(ByVal id As Integer) As IHttpActionResult
    If Id = 1 Then    
        Dim MyTelevisore As New Televisore    
        MyTelevisore.Colore = "grigio"
        MyTelevisore.Marca = "lg"
        Return Ok(MyTelevisore)
    Else            
        Dim MyCellulare As New Cellulare    
        MyCellulare.Colore = "nero"
        MyCellulare.SistemaOperativo = "android"    
        Return Ok(MyCellulare)
    End If    
End Function