azure - asp-page-handler 辅助标记未填充正确的超链接
问题描述
我在传递 ID 而不是处理程序的路由值时,asp-page-handler 填充正确的链接时遇到问题(所以“{int:id}”与“{handler?}”)。(见图片底部)。我期待这样的事情:
https://localhost:5001/Emp/Download?id=Matthew.pdf
在我的小型测试应用程序中,我将员工 ID 值硬编码到 中GetDirectoryReference("E000002/stubs/")
,它工作正常。(请注意,E000002 是根据登录的人而改变的值。该值确实填充在我的 OnGetAsync() 调用中,因此没有问题)。
客户界面 GetEmployeePayrollNo 的设置:
public async Task<Employee> GetEmployeePayrollNo(int id)
{
return await _context.Employees
.Include(e => e.EmployeePayroll)
.Where(p => p.EmployeeId == id)
.FirstAsync();
}
在这个测试中,我试图将一个变量传递给 GetDirectoryReference,具体取决于谁登录。
不确定我在搞砸什么,因为选择下载或查看甚至都没有进入调试模式。
使用 Azure 文件共享保存文档* 使用 Razor 页面构建文件夹结构*
- 页面
-
雇员
-
Paystubs.cshtml
Paystubs.cshtml 为登录的人获取一个 id 路由值。在我的测试应用程序中,它采用了一个“{handler?}”路由值。不知道能不能两个都用???
模型
private readonly IConfiguration _configuration;
private readonly ICustomer _customer;
public PaystubsModel(IConfiguration configuration,
ICustomer customer)
{
_configuration = configuration;
_customer = customer;
}
public Employee Employee { get; set; }
public List<AzureFileModel> AzureFileModel { get; private set; } = new List<AzureFileModel>();
public async Task OnGetAsync(int id)
{
Employee = await _customer.GetEmployeePayrollNo(id);
var empPayNo = Employee.EmployeePayroll;
string fileStorageConnection = _configuration.GetValue<string>("FileStorage");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("test");
CloudFileDirectory root = share.GetRootDirectoryReference();
CloudFileDirectory dir = root.GetDirectoryReference(empPayNo.EmployeeOnline+"/stubs");
// list all files in the directory
AzureFileModel = await ListSubDir(dir);
}
public static async Task<List<AzureFileModel>> ListSubDir(CloudFileDirectory fileDirectory)
{
// LEFT OUT FOR BREVITY
}
public async Task<IActionResult> OnGetDownload(string fileId)
{
var empPayNo = Employee.EmployeePayroll;
string fileStorageConnection = _configuration.GetValue<string>("FileStorage");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("test");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFileDirectory dir = rootDir.GetDirectoryReference(empPayNo.EmployeeOnline+"/stubs");
CloudFile file = dir.GetFileReference(fileId);
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);
}
}
页:
@page "{id:int}" //*******In my test model I was using {handler?} but I need to pass in the employee id to route here to display only the logged in employee. Again, not sure if its possible to use both?
@model NavraePortal.WebApp.Pages.Emp.PaystubsModel
@{
ViewData["Title"] = "Documents";
}
<h1>Pay Stub Copies</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>File Name</th>
<th>File Date</th>
<th>Download</th>
<th>View</th>
</tr>
</thead>
<tbody>
@foreach (var data in Model.AzureFileModel)
{
<tr>
<td>@data.FileName</td>
<td>@data.DateModified</td>
<td>
<a class="btn btn-primary" asp-route-id="@data.FileName" asp-page-handler="Download">Download</a>
</td>
<td>
<a class="btn btn-info" asp-route-id="@data.FileName" asp-page-handler="View">View</a>
</td>
</tr>
}
</tbody>
</table>
解决方案
推荐阅读
- javascript - 如何根据两个数组获取具有匹配计数的对象
- python - 如何使 Atom 中的脚本包正常工作?
- python - 使用 Python FastAPI ASGI 在 Azure 函数上进行路由
- c# - 如何使用 .net core 在 ckeditor 中实现电子邮件模板
- python-3.x - 使用调试编译 tensorflow 显示有关不存在的 bazel-out 的错误
- typescript - 打字稿索引签名失败Angular对象
- c# - 如何在 c# 中为 DateTime 添加自定义日期格式验证?
- javascript - d3.js 多系列区域图工具提示
- ios - 当应用程序在 iOS 应用程序中进入后台时,无法使用 AVAssetExportSession 导出音频
- node.js - 为什么 zsh 不运行 npm 脚本