簡體   English   中英

無法將類型“System.Web.Http.Results.BadRequestErrorMessageResult”隱式轉換為“System.Net.Http.HttpResponseMessage”

[英]Cannot implicitly convert type 'System.Web.Http.Results.BadRequestErrorMessageResult' to 'System.Net.Http.HttpResponseMessage'

我創建了一個 webapi get 方法來返回帶有 blob 存儲 URL 的圖像。

但是我也需要處理異常,在這種情況下我應該怎么做呢? 我在 catch 的最后一行得到一個錯誤

[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);
            }          
        }

嘗試這個

        [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);
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM