我有一个针对 AspNetCore 2.2 的 REST API,其端点允许下载一些大的 json 文件。迁移到 AspNetCore 3.1 后,此代码停止工作:
try
{
HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
HttpContext.Response.Headers.Add("Content-Type", "application/json");
using (var bufferedOutput = new BufferedStream(HttpContext.Response.Body, bufferSize: 4 * 1024 * 1024))
{
await _downloadService.Download(_applicationId, bufferedOutput);
}
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
这是下载方法,它创建了我想要在 HttpContext.Response.Body 上返回的 json:
public async Task Download(string applicationId, Stream output, CancellationToken cancellationToken = default(CancellationToken))
{
using (var textWriter = new StreamWriter(output, Constants.Utf8))
{
using (var jsonWriter = new JsonTextWriter(textWriter))
{
jsonWriter.Formatting = Formatting.None;
await jsonWriter.WriteStartArrayAsync(cancellationToken);
//write …Run Code Online (Sandbox Code Playgroud) 我收到一个XML文件,它在根节点上分配了一个xmlns命名空间:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message xmlns="http://www.myAddress.com/DataRequest/message/">
<Date>2017/01/01</Date>
</Message>
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用XPath检索Date元素,我试过了
var root = xDocument.Root;
var dateElement = root.XPathSelectElement("/Message/Date");
Run Code Online (Sandbox Code Playgroud)
如果我从根xml中删除命名空间,那么我可以使用"/ Message/Date"检索该值.
我试图将xmlns添加到XmlNamespaceManager,但是我收到此错误:
前缀"xmlns"保留供XML使用.
我怎样才能获得价值?
我正在使用 Azure.Storage.Blobs 版本 = 12.4.1。我有一个 REST 端点,我想用它从存储帐户下载 blob。
我需要将结果流式传输到HttpResponseMessage并且我不想使用 MemoryStream。我想将结果直接流式传输到调用客户端。有没有办法实现这一目标。如何在 HttpResponseMessage 内容中获取下载的 blob?我不想使用MemoryStream,因为会有很多下载请求。
BlobClient 类有一个方法 DownloadToAsync 但它需要一个 Stream 作为参数。
var result = new HttpResponseMessage(HttpStatusCode.OK);
var blobClient = container.GetBlobClient(blobPath);
if (await blobClient.ExistsAsync())
{
var blobProperties = await blobClient.GetPropertiesAsync();
var fileFromStorage = new BlobResponse()
{
ContentType = blobProperties.Value.ContentType,
ContentMd5 = blobProperties.Value.ContentHash.ToString(),
Status = Status.Ok,
StatusText = "File retrieved from blob"
};
await blobClient.DownloadToAsync(/*what to put here*/);
return fileFromStorage;
}
Run Code Online (Sandbox Code Playgroud)