首页 > 解决方案 > 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 为登录的人获取一个 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>

标签: azureasp.net-corerazorentity-framework-core

解决方案


因为 this idinasp-route-id与 不匹配fileId。在此页面中,您需要对其进行修改。

<a class="btn btn-primary" asp-route-fileId="@data.FileName" asp-page-handler="Download">Download</a>

然后,此 url 被更新。

在此处输入图像描述


推荐阅读