확장 메소드 IsJsonRequest()를 구현하고 싶습니다. bool HttpRequestBase 유형. 넓은 의미로이 메소드는 어떻게 보이고 참조 구현이 있습니까?ASP.NET, 요청 콘텐츠 유형이 JSON 용인지 확인하십시오.
개인 API입니다.
편집 :
제안; x-requested-with 헤더가 "xmlhttprequest"인지 확인하십시오.
확장 메소드 IsJsonRequest()를 구현하고 싶습니다. bool HttpRequestBase 유형. 넓은 의미로이 메소드는 어떻게 보이고 참조 구현이 있습니까?ASP.NET, 요청 콘텐츠 유형이 JSON 용인지 확인하십시오.
개인 API입니다.
편집 :
제안; x-requested-with 헤더가 "xmlhttprequest"인지 확인하십시오.
이 콘텐츠 유형과를 검사합니다 X-요청-으로 pretty much all javascript frameworks use 헤더 :
이public bool IsJsonRequest() {
string requestedWith = Request.ServerVariables["HTTP_X_REQUESTED_WITH"] ?? string.Empty;
return string.Compare(requestedWith, "XMLHttpRequest", true) == 0
&& Request.ContentType.ToLower().Contains("application/json");
}
개인용 API이므로 JSON의 콘텐츠 유형을 제어 할 수 있으므로 동의 된 값인지 확인하기 만하면됩니다.
public static bool IsJsonRequest(this HttpRequestBase request)
{
bool returnValue = false;
if(request.ContentType == "application/json")
returnValue = true;
return returnValue;
}
jQuery의이 $ 갔지이 콘텐츠를 전송되지 않았습니다 유형. 대신 "Application/json"에 대한 Request.AcceptTypes를 확인해야했습니다.
희망은 다른 사용자가 같은 문제를 겪을 때 도움이되기를 바랍니다.
참고 : 올바르게 읽으면 Content-Type 헤더가 ** 요청 **의 형식을 지정합니다 - 클라이언트가받는 것으로 기대되는 것과 아무런 관련이 없습니다 - 이는 Accept 헤더입니다. http : //en.wikipedia.org/wiki/List_of_HTTP_header_fields – Kobi