2013-10-22 13 views
0

모든 요청에 ​​있어야하는 사용자 지정 헤더를 확인하는 사용자 지정 메시지 처리기를 구현하고 싶습니다.ASP.NET 웹 API 메시지 처리기

내 사용자 정의 헤더가있는 경우 요청이 통과되어 헤더가 없으면 요청이 거부되고 사용자 정의 오류 메시지와 함께 요청이 거부됩니다.

아니요. 내 처리기를 구현하면 모든 요청에 ​​헤더가 있어야한다는 것을 의미합니다. 그러나 해당 헤더없이 호출 할 수있는 gate이 있어야하며 요청은 메시지 처리기에서 무시해야합니다. 컨트롤러를 사용자 지정 헤더 없이도 사용할 수 있습니다.

이것을 달성 할 수 있습니까? 또는 특정 컨트롤러 또는 특정 호출을 무시할 메시지 처리기를 구현하려면 어떻게해야합니까?

+1

당신은 아마이 경우 권한 필터 또는'경로 - specific' 메시지 핸들러를 사용할 수 있습니다. –

+0

나는 할 수있는이 문을 보지 못한다. "http handler"또는 "http module"또는 "action filter"에서 사용자 정의 헤더에 대해이 검사를 구현하면됩니다. 모든 통화에 대해 실행됩니다. 어쩌면 당신은 특정 쿼리 문자열이나 쿠키가 존재하는 것처럼 다른 조건을 추가 할 수 있습니다. 사용자 정의 헤더가 없어도 괜찮습니다. –

+0

사실 @KiranChalla가 옳다면 다음과 같은 경로 특정 메시지 처리기를 구현할 수 있습니다. http : // www. asp.net/web-api/overview/working-with-http/http-message-handlers – user2818430

답변

0

이 시도 할 수 있습니다 .. (안된)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Http; 


public abstract class EnforceMyBusinessRulesController : ApiController 
{ 

    protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext) 
    { 

     /* 

      Use any of these to enforce your rules; 

      http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller%28v=vs.108%29.aspx 

      Public property Configuration Gets or sets the HttpConfiguration of the current ApiController. 
      Public property ControllerContext Gets the HttpControllerContext of the current ApiController. 
      Public property ModelState Gets the model state after the model binding process. 
      Public property Request Gets or sets the HttpRequestMessage of the current ApiController. 
      Public property Url Returns an instance of a UrlHelper, which is used to generate URLs to other APIs. 
      Public property User Returns the current principal associated with this request. 
     */ 

     base.Initialize(controllerContext); 

     bool iDontLikeYou = true; /* Your rules here */ 
     if (iDontLikeYou) 
     { 
      throw new HttpResponseException(new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.NotFound)); 
     } 


    } 

} 



public class ProductsController : EnforceMyBusinessRulesController 
{ 

    protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext) 
    { 
     base.Initialize(controllerContext); 
    } 


} 
+0

나는 이것이 사용자가 요구하는 것들을위한 복잡한 접근이라고 생각한다. –

+0

내가 들었어. 간단한 (r) 응답을 제공하고, 나는 그것을 upvote거야! 나는 새로운 것을 배우기 위해 모든 것을 배우고 있습니다. – granadaCoder