我正在尝试获取 Razor 页面中区域页面的绝对 URI。我正在使用 LinkGenerator 的 GetUriByPage 方法。虽然这适用于不在区域内的页面,但当我尝试在应用程序中获取区域页面的 URI 时,它会返回 null。下面是我正在做的事情的一个例子。
var uri = _linkGenerator.GetUriByPage(HttpContext, "/SomeArea/Tickets/Index", null, new { CurrentFilter = "Open" });
我错过了什么?我没有看到 GetUriByAreaPage 方法。难道根本就无法做到这一点吗?
最佳答案
所以我找到了自己问题的答案,但会将其发布在这里,以防其他人遇到同样的问题。上https://www.learnrazorpages.com/advanced/areas有这个部分。
If using the RedirectToPage method to transfer someone to a location within an area, you should use the overload that takes an object representing route values to specify the area:
public IActionResult OnGet() { return RedirectToPage("/Create", new { area = "Administration" }); }
我将相同的逻辑应用于 GetUriByPage。下面是我之前的示例,但确实有效。
var uri = _linkGenerator.GetUriByPage(HttpContext, "/Tickets/Index", null, new { Area = "SomeArea", CurrentFilter = "Open" });
关于c# - LinkGenerator GetPathByPage 对于区域页面返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71575024/