Не удается неявно преобразовать тип 'System.Web.Http.Results.BadRequestErrorMessageResult' в 'System.Net.Http.HttpResponseMessage' - PullRequest
0 голосов
/ 17 октября 2019

Я создал метод get webapi для возврата изображений с 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 Ответ

1 голос
/ 17 октября 2019

попробуйте

        [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);
            }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...