c# - URL 格式无法识别,我可以转换吗

标签 c# azure

我想显示以字符串形式存储在数据库表中的图像。当我运行代码时,出现错误“无效 URI,无法确定格式”。在表中,实际的字符串是这样的:13d2dr09-377-423c-993e-22db3l390b66

如何转换它以便可以识别它。

                string sAdImageUrl = myReader.GetString(3);

                var image = new BitmapImage();
                int BytesToRead = 100;

                WebRequest request = WebRequest.Create(new Uri(sAdImageUrl,UriKind.Absolute));                   
                request.Timeout = -1;
                WebResponse response = request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                BinaryReader reader = new BinaryReader(responseStream);
                MemoryStream memoryStream = new MemoryStream();

                byte[] bytebuffer = new byte[BytesToRead];
                int bytesRead = reader.Read(bytebuffer, 0, BytesToRead);

                while (bytesRead > 0)
                {
                    memoryStream.Write(bytebuffer, 0, bytesRead);
                    bytesRead = reader.Read(bytebuffer, 0, BytesToRead);
                }

                image.BeginInit();
                memoryStream.Seek(0, SeekOrigin.Begin);

                image.StreamSource = memoryStream;
                image.EndInit();

                imaPartners.Source = image;

            }
        }

最佳答案

好的,根据您的问题和另一个答案中的评论,听起来您有 blob 名称,但没有完整的 URI。完整的 blob uri 将是

http(s)://<cloudstorageaccountname>.blob.core.windows.net/<containername>/<blobname>

由于您已经在使用容器对象,因此您必须已经获取存储帐户名称(可能来自您的应用设置之一)以及容器名称(因为您已经有一个容器对象)。

此时,您应该能够轻松组合出全名。请注意,您可以选择 http 或 https。如果您从 Web/应用程序层直接连接到存储,请使用 http,因为流量保留在数据中心内。另一方面,如果您要创建嵌入到最终用户网页中的链接,并且数据具有任何敏感性,则应考虑使用 https。

您可以通过代表您的 blob 的 CloudBlockBlob 对象轻松获取完整的 Uri。作为一个简单的示例,下面是一个控制台应用程序的片段,演示了这一点:

        var connString = CloudConfigurationManager.GetSetting("connectionString");
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connString);

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("images");
        var blob = container.GetBlockBlobReference("fr7_20_2013110753_jpg.jpg");
        var uri = blob.Uri;
        Console.WriteLine(uri);
        Console.ReadLine();

输出:

full url

关于c# - URL 格式无法识别,我可以转换吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18257501/

相关文章:

azure - 对 azure devops 的评论的永久链接(在拉取请求中)

c# - 在 C# 中使用外部 C++ 库

c# - 在 .NET 中将域名转换为 LDAP 样式

azure - 在 Azure 中的存档 blob 上设置元数据

azure - 对于 Azure 静态网站,是否有办法识别资源被访问的次数?

azure - 我看到 Azure 应用服务的诊断和解决中报告了 502 错误

c# - 如何知道用户账号是否存在

c# - Json对象从YouTube返回重复字段

c# - 如何在 kinect 中实现挥手手势?

azure - 容器的 Web 应用程序 - Http 503