Azure文件共享-找不到文件404,即使它列出了我的应用程序中的文件

问题描述

也许我在方法调用错误的Task动作,因为它在MVC项目上正常工作。严格来说,这是尝试在Razor Page而不是MVC上使用它。

当我调用OnGetAsync()时,我的页面确实填充了所有可用文件。但是,当我尝试下载文件时,它显示找不到文件

DownloadStub方法

public async Task<IActionResult> DownloadStub(string id)
        {
            string fileStorageConnection = _configuration.GetValue<string>("fileStorageConnection");
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
            CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("test");
            CloudFileDirectory rootDir = share.GetRootDirectoryReference();
            CloudFileDirectory dir = rootDir.GetDirectoryReference(@"E000002/stubs");
            CloudFile file = dir.GetFileReference(id);

            if (!file.Exists())
            {
                ModelState.AddModelError(string.Empty,"File not found.");
                return Page();
            }
            else
            {
                await file.DownloadToStreamAsync(new MemoryStream());
                Stream fileStream = await file.OpenReadAsync();
                return File(fileStream,file.Properties.ContentType,file.Name);
            }

        }

cshtml页面

<td>
    <a class="btn btn-primary" href="~/Files/DownloadStub?id=@data.FileName">Download</a>
</td>

当我尝试在该方法上设置一个断点时,它不会被命中,我认为这是问题的一部分,但我不知道如何进一步调查。

如果有帮助,请查看此页面you can review this post上的其他实现方法

View of files being returned.

Folders

解决方法

对于剃须刀页面,剃须刀页面中的页面方法名称与mvc中的操作方法不同。例如:OnGet MethodName 用于获取方法,OnPost MethodName 用于发布方法。

参考:

https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-3.1&tabs=visual-studio#multiple-handlers-per-page

按如下所示更改您的剃须刀页面:

<td>
    <a class="btn btn-primary" href="~/Files?id=@data.FileName&handler=DownloadStub">Download</a>
</td>

后端代码:

public class IndexModel : PageModel
{
    public void OnGet()
    {
       //...
    }
    public void OnGetDownloadStub(string id)
    {
       //do your stuff...
    }
}

结果: enter image description here