2012-12-11 3 views
5

내가 만들고있는 사이트에서 referer를 사용하여 AJAX 요청이 올바른 URL에서 오는 것인지 확인하려고합니다.Sitecore에서 URL과 일치하는 항목 가져 오기

이렇게하려면 Sitecore에서 URL을 항목으로 확인하고 싶습니다. 예를 들어,

http://www.mysite.com/abc/def

경로

sitecore/Content/MySite/Home/abc/def

내 코드에서 이것에 대해 갈 수있는 권장되는 방법은 무엇에있는 항목으로 해결할 수 있는가?

답변

1

sitecore/content/MySite/Home/abc/def 항목을 해결하게하려면 다음과 같이 web.config에 <site>을 구성해야합니다. (예 : AJAX 요청과 함께)

<site name="MySite" hostName="www.mysite.com" rootPath="/sitecore/content/MySite" startItem="/Home" *other attributes here* /> 
1

당신은 (전체) 경로의 기반으로 항목을 해결하려면 방법 ItemManager.GetItem(itemPath, language, version, database, securityCheck)를 사용할 수 있습니다.

6

모든 답변을 주셔서 감사합니다.하지만 그들 중 누구도 내가 필요한 모든 것을하지 않았습니다. 이것은 나를 위해 일했다.

var url = new Uri(...); 

// Obtain a SiteContext for the host and virtual path 
var siteContext = SiteContextFactory.GetSiteContext(url.Host, url.PathAndQuery); 

// Get the path to the Home item 
var homePath = siteContext.StartPath; 
if (!homePath.EndsWith("/")) 
    homePath += "/"; 

// Get the path to the item, removing virtual path if any 
var itemPath = MainUtil.DecodeName(url.AbsolutePath); 
if (itemPath.StartsWith(siteContext.VirtualFolder)) 
    itemPath = itemPath.Remove(0,siteContext.VirtualFolder.Length); 

// Obtain the item 
var fullPath = homePath + itemPath; 
var item = siteContext.Database.GetItem(fullPath); 
+0

다국어 시스템의 경우에는 제대로 작동하지 않을 수 있습니다. ?? – theusguy

+0

가상 폴더에 하이픈 ('-')이 있었기 때문에 경로 시작 부분에 가상 폴더가 있는지 확인한 후 호출을'DecodeName'으로 이동해야했습니다. @theusguy URL에서 GetItem을 추출한 경우 (또는 사용자가 sc_lang 쿠키에서 읽은 경우) 언어로 GetItem을 호출 할 수도 있습니다. –