2012-12-25 2 views
4

maxRequestLength이라는 구성 값이 있습니다. 설정 파일에서 그것은 다음과 같습니다프로그래밍 방식으로 maxRequestLength 설정

<configuration> 
    <system.web> 
    <httpRuntime maxRequestLength="2048576" /> 
    </system.web> 
</configuration> 

어떻게 프로그래밍 maxRequestLength의 값을 설정할 수 있습니까?

답변

6

수 없습니다!

maxRequestLength

는 요청 는 대응 asp.net 작업자에 의해 처리 한 서버를 친 후 일반 핸들러 또는 페이지가 실행되는 것을 의미 실제 HttpHandlerHttpWorkerRequest 이전 호출에 의해 처리된다. 당신은 귀하의 페이지 코드 또는 HttpHandler에서 maxRequestLength을 제어 할 수 없습니다!

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    IServiceProvider provider = (IServiceProvider)HttpContext.Current; 
    HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); 

    if (workerRequest.HasEntityBody()) 
    { 
     long contentLength = long.Parse((workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength))); 
    } 
} 

당신은을 설정할 수 있습니다 : 당신이 할 수있는 코드의 요청 길이를 읽으려면

이 중 하나 HttpModule 또는 global.asax 파일을 통해,이 그것이 Global.asax에 내부에서 수행되는 방법이다 의 maxRequestLength을 최대 값으로 설정하고 요청 길이가 원하는 값에 도달하면 코드에서 worker의 CloseConnection 메서드를 호출하십시오!

+0

다음은 설정을 읽는 더 쉬운 방법입니다. http://stackoverflow.com/questions/4887016/how-to-access-httpruntime-section-of-web-config-from-codebehind –

+0

@MikeSchall OP는 가능한 maxRequestLength를 변경하는 방법을 알려줍니다. 코드 스 니펫 위의 내용은 max-length가 아닌 모든 요청의 content-length를 읽는 방법입니다. –

3

빠른 Google 후에 프로그램 적으로 할 수없는 것으로 보입니다. See here.

두 가지 가능한 솔루션 :

  1. 를 사용하여 지역화 된 Web.config의 특정 디렉토리를 구성 할 수 있습니다.
  2. web.config의 <location> 요소를 사용하여 특정 경로를 구성하십시오.
관련 문제