2011-11-25 6 views
4

web.config customErrors 섹션에 대한 참조를 얻으려고합니다. 다음 코드를 사용하면 항상 null이됩니다. 내가 만들었던 커스텀 섹션에 대한 참조를 얻을 때이 문제가 발생하지 않아서 이것이 왜 작동하지 않는지 조금 어이가 있습니다.ASP.NET web.config customErrors 섹션에 대한 참조 얻기

CustomErrorsSection customErrorSection = 
    ConfigurationManager.GetSection("customErrors") as CustomErrorsSection; 

나는 또한이 시도했습니다

CustomErrorsSection customErrorSection = 
    WebConfigurationManager.GetSection("customErrors") as CustomErrorsSection; 

나는이 시도했습니다 :

CustomErrorsSection customErrorSection = 
    WebConfigurationManager.GetWebApplicationSection("customErrors") as CustomErrorSection; 

편집 :

아으! 질문을 한 직후에 대답을 찾은 대부분의 것들이 그러한 경우입니다.

나를 위해 작동합니다

System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/"); 
CustomErrorsSection customErrorsSection = (CustomErrorsSection)configuration.GetSection("system.web/customErrors"); 

또는 더 간단하게 다음과 같이 :

CustomErrorsSection customErrors = (CustomErrorsSection) WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors"); 

를 이것은 또한 작동합니다

CustomErrorsSection customErrorsSection = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection; 

그래서 나는 내가 있었다 왜 이제 이해 생각 문제가 처음이다. 나는 내가 customErrors 섹션에 대한 참조를 GetSection ("customErrors")을 시도함으로써 얻을 수 있다고 잘못 생각 했었지만 그것이 실제로 어떤 루트 섹션에 살았는지에 대해 알려주지 못했고, 내가 어떻게 알았는지에 대한 나의 시도를 근거로 삼았다. 내 커스텀 섹션이 섹션의 루트라는 사실을 깨닫지 못했을 때 커스텀 섹션을 얻어서 GetSection()을 호출 할 때 system.Web/앞에 앞에 붙일 필요는 없었습니다.

+0

아마 바보 같은 질문을하지만, 섹션 설정 파일에 존재 하는가? – Oded

+0

예 섹션이 있습니다. –

+1

정당하고 다른 사람들을 도울 수 있습니다. 삭제하지 마십시오. – Oded

답변

9

이 시도 :

var configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); 

// Get the section. 
CustomErrorsSection customErrors = 
    (CustomErrorsSection)configuration.GetSection("system.web/customErrors"); 

여기에 주제에 대한 자세한 : CustomError Class

+1

감사합니다. 잠시 동안이 기사를 검색해 보니 게시 직후 Microsoft 페이지로 돌아가서 페이지를 넘은 처음 몇 번 스크롤하지 않았다는 것을 알았습니다. 때때로 당신은 너무나 좌절감을 느껴서 당신이 찾고있는 자료를 해석 할만큼 명확하게 생각할 수없는 대답을 찾으려고 노력합니다. –

+2

답변을 올바른 것으로 표시 하겠지만 코드 스 니펫은 링크 된 솔루션의 일부만 보여줍니다. 구성의 출처를 선언하는 부분을 붙여 넣지 않았습니다. –

+0

구성 = WebConfigurationManager.OpenWebConfiguration ("~/Web.config"); CustomErrorsSection – Darren