【ASP.NET】 ajax post で、404エラーとなってしまう

今日はajax postで長文を送った場合にエラーになってしまった場合の対策備忘録です。
サーバはWindows IISです。

今までは普通に送れていたのに、長文にした時にエラーになってしまう場合は、IISにはじかれていると考えられます。
ちなみにデフォルトは何バイトか調べて居ません。

で、まずはエラーの内容確認

$.ajax({
   		type: "Post",
   		url: "************",
		contentype: "application/json"
		dataType:"json",
   		success: function(msg){
			document.open();
			document.write(msg);
			document.close();
   		},
   		error: function(XMLHttpRequest, textStatus, errorThrown){
			msg="--- Error Status ---"
			msg=msg+"<BR>"+"status:"+XMLHttpRequest.status;
			msg=msg+"<BR>"+"statusText:"+XMLHttpRequest.statusText;
			msg=msg+"<BR>"+"textStatus:"+textStatus;
			msg=msg+"<BR>"+"errorThrown:"+errorThrown;
			for(var i in errorThrown)
			{
				msg=msg+"<BR>"+"error "+i+":"+ errorThrown[i];
			}
			document.open();
			document.write(msg);
			document.close();
   		}
	});

エラーコードは次のような感じです。
 404.13 : コンテンツの長さが長すぎます
 404.14 : URL が長すぎます
 404.15 : クエリ文字列が長すぎます

各々は以下のパラメータで対応できます。
 maxAllowedContentLength
 maxUrl
 maxQueryString

具体的には、web.congigの中に下のような感じで記述します。

<configuration>
 <system.webServer>
  <security>
   <requestFiltering>
    <requestLimits
           maxAllowedContentLength="30000000"       
	   maxUrl="300"       
	   maxQueryString="10000"/>   
	</requestFiltering>
  </security>
 </system.webServer>
</configuration>

で、今回の備忘録はajax postで長文を送った場合にエラーとなった場合への対応備忘録ですので、コンテンツが長い場合、URLが長い場合は調べて居ませんが、
クエリー文字列が長い場合、上の対応をしたとしても、実はまだエラーになります。

※クエリー文字列が長すぎる旨のエラーになってしまいます。

で、どうすれば良いかと言うともう一つ「maxQueryStringLength」パラメータを設定してあげます。
やはり、web.configの中で設定するのですが、

<configuration>
  <system.web>
    <httpRuntime maxQueryStringLength="10000" />
  </system.web>
</configuration>

私の環境では、

<configuration>
  <system.web>
    <httpRuntime targetFreamework="4.5" maxQueryStringLength="10000" />
  </system.web>
</configuration>

となりました。

これで長い文字列を含むajax Postでも正常にIISで受け付けてもらえました。

以上、ajax postで長文を送った場合にエラーとなった場合への対応備忘録でした。