asp.net - 使用查询参数时 SiteMap.CurrentNode 返回 null

标签 asp.net virtualpathprovider

我编写了一个自定义 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/

相关文章:

javascript - 使用 javascript 将下拉列表值传递到文本框

c# - 将 SignalR 用户映射到组中的连接

c# - 虚拟路径提供程序禁用缓存?

带有静态文件问题的 ASP.NET VirtualPathProvider

asp.net - 无法调试通过自定义 VirtualPathProvider 加载的 EmbeddedResource View

ASP.NET 成员角色

c# - 在 c# 中仅使用 getter 和 List 时出现问题

asp.net - Web部署项目: Publish without Precompilation

asp.net - 什么是 ASP.NET Web 应用程序项目中的 UseCustomServer 和 Use Custom Server Url?