'TestController.Post()':并非所有代码路径都返回值 [英] 'TestController.Post()': not all code paths return a value

查看:78
本文介绍了'TestController.Post()':并非所有代码路径都返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在返回 HttpResponseMessage ,但是我的代码仍然抱怨并抛出错误,

I am returning HttpResponseMessage, but still my code complains and throw below error,

'TestController.Post()':并非所有代码路径都返回值

'TestController.Post()': not all code paths return a value

这里返回什么?

public async Task<IHttpActionResult> Post()
    {
        var response = await IsDataValid();
        if (response)
            return Ok(new HttpResponseMessage(HttpStatusCode.OK));
    }

推荐答案

您只需要再添加几行代码即可解决上述错误.

You need to add just few more line of code to deal with the above error.

public async Task<IHttpActionResult> Post()
{
    var response = await IsDataValid();
    if (response)
        return Ok(new HttpResponseMessage(HttpStatusCode.OK));
    else
        return NotFound();
}

其他部分在这里很重要.

The else part is important here.

这篇关于'TestController.Post()':并非所有代码路径都返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆