无法将类型'System.Web.Http.Results.BadRequestErrorMessageResult'隐式转换为'System.Net.Http.HttpResponseMessage'

时间:2019-10-17 08:42:09

标签: c# asp.net .net asp.net-web-api

我创建了一个webapi get方法来返回带有Blob存储URL的图像。

但是我也需要处理异常,在这种情况下应该如何处理?在捕获的最后一行出现错误

[HttpGet]
        public async Task<HttpResponseMessage> GetProfileImage(string url)
        {
            var telemetry = new TelemetryClient();

            try
            {
                //Initalize configuration settings
                var accountName = ConfigurationManager.AppSettings["storage:account:name"];
                var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
                var profilepicturecontainername = ConfigurationManager.AppSettings["storage:account:profilepicscontainername"];

                //Instance objects needed to store the files
                var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer imagesContainer = blobClient.GetContainerReference(profilepicturecontainername);

                //Gets blob reference
                var cloudBlob = await blobClient.GetBlobReferenceFromServerAsync(new Uri(url));

                //Check if it exists
                var cloudBlobExists = await cloudBlob.ExistsAsync();

                //Opens memory stream
                using (MemoryStream ms = new MemoryStream())
                {
                    cloudBlob.DownloadToStream(ms);
                    HttpResponseMessage response = new HttpResponseMessage();
                    response.Content = new ByteArrayContent(ms.ToArray());
                    response.Content.LoadIntoBufferAsync(b.Length).Wait();
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                    return response;
                }
            }
            catch (Exception ex)
            {
                string guid = Guid.NewGuid().ToString();
                var dt = new Dictionary<string, string>
                {
                    { "Error Lulo: ", guid }
                };

                telemetry.TrackException(ex, dt);
                return BadRequest("Error Lulo: " + guid);
            }          
        }

1 个答案:

答案 0 :(得分:1)

尝试

        [HttpGet]
        public async Task<IActionResult> GetProfileImage(string url)
        {
            var telemetry = new TelemetryClient();

            try
            {
                //Initalize configuration settings
                var accountName = ConfigurationManager.AppSettings["storage:account:name"];
                var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
                var profilepicturecontainername = ConfigurationManager.AppSettings["storage:account:profilepicscontainername"];

                //Instance objects needed to store the files
                var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer imagesContainer = blobClient.GetContainerReference(profilepicturecontainername);

                //Gets blob reference
                var cloudBlob = await blobClient.GetBlobReferenceFromServerAsync(new Uri(url));

                //Check if it exists
                var cloudBlobExists = await cloudBlob.ExistsAsync();

                //Opens memory stream
                using (MemoryStream ms = new MemoryStream())
                {
                    cloudBlob.DownloadToStream(ms);

                    return File(ms, "application/octet-stream");
                }
            }
            catch (Exception ex)
            {
                string guid = Guid.NewGuid().ToString();
                var dt = new Dictionary<string, string>
                {
                    { "Error Lulo: ", guid }
                };

                telemetry.TrackException(ex, dt);
                return BadRequest("Error Lulo: " + guid);
            }
        }