不能隐式地将类型'system.net.http.httpresponsemessage'转换为'system.web.httpresponse'

时间:2017-09-26 09:14:54

标签: c# mysql asp.net-mvc rest asp.net-web-api2

我使用web service数据库在visual studio 2015中创建MySQL。我想只使用HTTP实现GET方法。现在,当我这样做的时候,我突然发现错误。这是

cannot implicitly convert type 'system.net.http.httpresponsemessage' to 'system.web.httpresponse'

它到底的地方是

public HttpResponse Get()
    {
        try
        {
            return  Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.ToList());
        }
        catch (Exception)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Data found");
        }
    }

下面是图片

enter image description here

我找不到它的解决方案。任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

直接它不会像预期的那样进行隐式转换,但是我们可以使用HttpResponseMessage / HttpWebResponse类型的函数来避免上述问题。我已经提到了一些示例代码,可能会对您有所帮助。

[HttpPost]
        [Route("AddorUpdatePersonalInfo")]
        public async Task<HttpResponseMessage> AddorUpdatePersonalInfo([FromBody]PersonalInfo personalinfo)
        {
            try
            {
                var result = await this._Personalinforepository.AddorUpdatePersonalInfo(personalinfo);
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, result);
                return response;
            }
            catch(Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }