我编写了一个自定义 ASP.NET 站点地图提供程序,它运行良好,但如果我将查询参数添加到虚拟路径 SiteMap.CurrentNode
返回 null
- 它确实找不到页面。我已经在所有代码中放置了断点,并且它从未使用查询参数进入我的虚拟路径提供程序。我在这里缺少什么?
最佳答案
我找到了问题的答案并将其发布在这里以供以后使用。似乎站点地图提供程序在查找匹配路径时始终使用不带查询字符串参数的路径。诀窍是不要在重写的 SiteMapProvider.CurrentNode()
函数中使用 Reqest.RawUrl
,而是使用 Request.Path
;我在下面发布了我的解决方案:
public class CustomSiteMapProvider : SiteMapProvider {
// Implement the CurrentNode property.
public override SiteMapNode CurrentNode {
get {
var currentUrl = FindCurrentUrl();
// Find the SiteMapNode that represents the current page.
var currentNode = FindSiteMapNode(currentUrl);
return currentNode;
}
}
// Get the URL of the currently displayed page.
string FindCurrentUrl() {
try {
// The current HttpContext.
var currentContext = HttpContext.Current;
if (currentContext != null) return currentContext.Request.Path;
throw new Exception("HttpContext.Current is Invalid");
} catch (Exception e) {
throw new NotSupportedException("This provider requires a valid context.", e);
}
}
...
关于asp.net - 使用查询参数时 SiteMap.CurrentNode 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26167393/