2012-08-11 11 views
8

ASP.Net MVC 3.0のHttpStatusCodeResultを使用して、StatusDescriptionを改行で返すと、クライアントへの接続が強制的に閉じられます。アプリケーションはIIS 7.0でホストされています。ASP.NetのStatusDescriptionに改行を追加すると接続が切断されるのはなぜですか?

例コントローラ:

public class FooController : Controller 
{ 
    public ActionResult MyAction() 
    { 
     return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest, "Foo \n Bar"); 
    } 
} 

例クライアント:

using (WebClient client = new WebClient()) 
{ 
    client.DownloadString("http://localhost/app/Foo/MyAction"); 
} 

スローされた例外:カール(カール7.25.0(I386-PCを使用する場合

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. 
    System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. 
    System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 

挙動が一致しています-win32)libcurl/7.25.0 zlib/1.2.6)

curl http://localhost/app/Foo/MyAction

カール:(56)のrecv失敗:接続がリセットされた

編集

私が望む結果を得るために、このカスタムのActionResultを使用して終了。

public class BadRequestResult : ActionResult 
{ 
    private const int BadRequestCode = (int)HttpStatusCode.BadRequest; 
    private int count = 0; 

    public BadRequestResult(string errors) 
     : this(errors, "") 
    { 
    } 

    public BadRequestResult(string format, params object[] args) 
    { 
     if (String.IsNullOrEmpty(format)) 
     { 
     throw new ArgumentException("format"); 
     } 

     Errors = String.Format(format, args); 

     count = Errors.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length; 
    } 

    public string Errors { get; private set; } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
     { 
     throw new ArgumentNullException("context"); 
     } 

     HttpResponseBase response = context.HttpContext.Response; 
     response.TrySkipIisCustomErrors = true; 
     response.StatusCode = BadRequestCode; 
     response.StatusDescription = String.Format("Bad Request {0} Error(s)", count); 
     response.Write(Errors); 
     response.End(); 
    } 
} 

答えて

10

HTTPヘッダーの途中で折れ線を付けることはできません。

HTTPプロトコルは、改行をヘッダーの末尾に指定します。

改行はヘッダーの中央にあるため、ヘッダーは有効なヘッダーではなく、このエラーが発生しています。

修正:HTTPヘッダーの途中に改行を入れないでください。

+0

大きな簡潔な答え。 – JJS

関連する問題