问题描述
也许我在方法上调用了错误的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上的其他实现方法。
解决方法
对于剃须刀页面,剃须刀页面中的页面方法名称与mvc中的操作方法不同。例如:OnGet MethodName 用于获取方法,OnPost MethodName 用于发布方法。
参考:
按如下所示更改您的剃须刀页面:
<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...
}
}