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
Asp.net 当odatav4方法返回SingleResult时,我应该返回什么<;T>;什么也没找到?_Asp.net_Odata_Asp.net Web Api2 - Fatal编程技术网

Asp.net 当odatav4方法返回SingleResult时,我应该返回什么<;T>;什么也没找到?

Asp.net 当odatav4方法返回SingleResult时,我应该返回什么<;T>;什么也没找到?,asp.net,odata,asp.net-web-api2,Asp.net,Odata,Asp.net Web Api2,我在ODataV4项目中有一个返回SingleResult的控制器方法。它查询一个表,当表中有数据时,该表返回它应该返回的内容。但是,当应用程序是新的且表为空时,会出现一个无法描述的错误: "'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter." 我的方法如下所示: [EnableQuery] public virtual SingleResult<LoggerEntry>

我在ODataV4项目中有一个返回SingleResult的控制器方法。它查询一个表,当表中有数据时,该表返回它应该返回的内容。但是,当应用程序是新的且表为空时,会出现一个无法描述的错误:

"'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter."
我的方法如下所示:

    [EnableQuery]
    public virtual SingleResult<LoggerEntry> Get([FromODataUri] int key)
    {
        using (var gateway = this.Gateway.GetChild())
        {
            var model = gateway.GetLoggerEntry(key);
            // TODO: Check for null and tell the client NotFound
            return SingleResult.Create((new List<LoggerEntry>(){ model }).AsQueryable());
        }
    }
[启用查询]
公共虚拟单结果获取([FromODataUri]int-key)
{
使用(var gateway=this.gateway.GetChild())
{
var model=gateway.GetLoggerEntry(key);
//TODO:检查null并告诉客户端NotFound
返回SingleResult.Create((new List(){model}).AsQueryable());
}
}

因为我使用的是SingleResult,所以我不能只
返回NotFound()。我试图使用一个空列表,但这给了我同样的错误。我找不到在这种情况下返回什么的示例或文档。我应该返回什么。

保留原始代码并尝试使用ODataNullValueMessageHandler

        configuration.MapODataServiceRoute(
            "odata", 
            "odata", 
            GetEdmModel(configuration),
            defaultHandler: HttpClientFactory.CreatePipeline(innerHandler: new HttpControllerDispatcher(configuration), handlers: new[] { new ODataNullValueMessageHandler()}));

保留原始代码并尝试使用ODataNullValueMessageHandler

        configuration.MapODataServiceRoute(
            "odata", 
            "odata", 
            GetEdmModel(configuration),
            defaultHandler: HttpClientFactory.CreatePipeline(innerHandler: new HttpControllerDispatcher(configuration), handlers: new[] { new ODataNullValueMessageHandler()}));

正如您正确地指出的,我们可能希望向用户报告一些可能不同的响应,200和404是不够的

我们可以添加处理程序或过滤器来解释异常并返回更有意义的http响应代码,如其他答案中所述,但当您想要显式返回不同的http响应代码(如400 BadRequest或404 NotFound)时,这仍然没有帮助

如果您发现自己抛出任意异常只是为了让另一个进程能够解释消息并将其重新格式化为可用的响应或更有意义的内容,这就是我们首先抛出异常的原因,那么您应该首先评估异常是否是表示响应的最佳机制

通过将web方法的响应类型更改为
IHttpActionResult
,我们可以对异常代码响应进行更具表现力的处理。在大多数情况下,此更改将与Web API运行时的其余部分兼容

现在,在您的方法中,我们可以轻松地表达不同的常见响应,如果您的控制器类继承自
ApiController
,那么下面的示例应该可以工作:

[EnableQuery]
public virtual IHttpActionResult Get([FromODataUri] int key)
{
    // HACK: Example of a specific case that we want to disallow
    if(key <= 0)
        return BadRequest("Keys must be positive values");

    using (var gateway = this.Gateway.GetChild())
    {
        var model = gateway.GetLoggerEntry(key);

        // Check for null and tell the client NotFound
        if(model == null)
            return NotFound();

        // NOTE: SingleResult does not implement IHttpActionResult and the ApiController 
        // does not provide a simple helper for SingleResult, so I've added one for you.
        return Single((new List<LoggerEntry>(){ model }).AsQueryable());
    }
}

/// <summary>
/// Helper to Create a SingleResult (200 OK) Response as IHttpActionResult
/// </summary>
/// <typeparam name="T">The type of item in query.</typeparam>
/// <param name="itemQuery">The query to get the item.</param>
/// <returns>A System.Web.Http.Results.JsonResult`1 with the specified values.</returns>
protected IHttpActionResult Single<T>(IQueryable<T> itemQuery)
{
    return Content(HttpStatusCode.OK, System.Web.Http.SingleResult.Create(itemQuery));
}
[启用查询]
公共虚拟IHttpActionResult Get([FromODataUri]int key)
{
//黑客:我们想禁止的一个具体案例的例子

如果(key如您正确识别的,我们可能希望向用户报告一些潜在的不同响应,200和404是不够的

我们可以添加处理程序或过滤器来解释异常并返回更有意义的http响应代码,如其他答案中所述,但当您想要显式返回不同的http响应代码(如400 BadRequest或404 NotFound)时,这仍然没有帮助

如果您发现自己抛出任意异常只是为了让另一个进程能够解释消息并将其重新格式化为可用的响应或更有意义的内容,这就是我们首先抛出异常的原因,那么您应该首先评估异常是否是表示响应的最佳机制

通过将web方法的响应类型更改为
IHttpActionResult
,我们可以在异常代码响应中更具表现力。在大多数情况下,此更改将与web API运行时的其余部分兼容

现在,在您的方法中,我们可以轻松地表达不同的常见响应,如果您的控制器类继承自
ApiController
,那么下面的示例应该可以工作:

[EnableQuery]
public virtual IHttpActionResult Get([FromODataUri] int key)
{
    // HACK: Example of a specific case that we want to disallow
    if(key <= 0)
        return BadRequest("Keys must be positive values");

    using (var gateway = this.Gateway.GetChild())
    {
        var model = gateway.GetLoggerEntry(key);

        // Check for null and tell the client NotFound
        if(model == null)
            return NotFound();

        // NOTE: SingleResult does not implement IHttpActionResult and the ApiController 
        // does not provide a simple helper for SingleResult, so I've added one for you.
        return Single((new List<LoggerEntry>(){ model }).AsQueryable());
    }
}

/// <summary>
/// Helper to Create a SingleResult (200 OK) Response as IHttpActionResult
/// </summary>
/// <typeparam name="T">The type of item in query.</typeparam>
/// <param name="itemQuery">The query to get the item.</param>
/// <returns>A System.Web.Http.Results.JsonResult`1 with the specified values.</returns>
protected IHttpActionResult Single<T>(IQueryable<T> itemQuery)
{
    return Content(HttpStatusCode.OK, System.Web.Http.SingleResult.Create(itemQuery));
}
[启用查询]
公共虚拟IHttpActionResult Get([FromODataUri]int key)
{
//黑客:我们想禁止的一个具体案例的例子

if(key)问题是它返回'Sequence contains no elements'错误。虽然这可能是SingleResult的预期功能?我想澄清一下,它会返回一个InvalidOperationException和一个500。我认为用404返回一些更有用的东西会更好。我只是不知道是什么。问题是它返回“Sequence contains no elements”错误。我宁愿返回一些东西类似于NotFound()。虽然这可能是SingleResult的预期功能?我想澄清一下,它会返回一个InvalidOperationException和一个500。我想最好用404返回一些更有用的东西。我只是不知道是什么。