2014-10-02 7 views
2

사용자 지정 ASP.NET 사이트 맵 공급자를 작성했지만 올바르게 작동하지만 쿼리 매개 변수를 가상 경로에 추가하면 SiteMap.CurrentNodenull을 반환합니다. 페이지를 찾지 못합니다. 모든 코드에 중단 점을 넣었으며 쿼리 매개 변수로 내 가상 경로 공급자를 입력하지도 않습니다. 내가 여기서 무엇을 놓치고 있니?SiteMap.CurrentNode는 쿼리 매개 변수를 사용할 때 null을 반환합니다.

답변

2

내 질문에 대한 답변을 발견하고 나중에 사용할 수 있도록 게시합니다. 일치하는 경로를 조회 할 때 사이트 맵 공급자가 항상 쿼리 문자열 매개 변수없이 경로를 사용하는 것으로 보입니다. 트릭은 오버라이드 된 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); 
     } 
    } 
    ...