2012-08-11 15 views
8

HttpStatusCodeResult ASP.Net MVC 3.0 kullanarak bir satırsonu ile bir StatusDescription döndüğümde, istemcim bağlantısı zorla kapatılır. Uygulama, IIS 7.0'da barındırılmaktadır.ASP.Net'deki StatusDescription öğesine yeni satır eklemek neden bağlantıyı kapatıyor?

Örnek kontrol:

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

Örnek istemcisi:

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

atılmış durum: bükülmesi (kıvrım 7.25.0 (i386 pc kullanıldığında

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 

davranış tutarlıdır -win32) libcurl/7.25.0 zlib/1.2.6)

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

bukle: (56), Alınan hatası: Bağlantı sıfırlandı

Düzenleme

Ben istenen sonuçları almak için bu özel ActionResult kullanarak sona erdi.

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(); 
    } 
} 

cevap

10

HTTP üstbilgisinin ortasında bir çizgi çizginiz olamaz.

HTTP protokolü, üstbilginin sonuna satır sonu olduğunu belirtir.

Satır sonu bir üstbilginin ortasında olduğundan, başlık geçerli bir üstbilgi değil ve bu hatayı alıyorsunuz.

Düzeltme: HTTP üstbilgisinin ortasına bir satır sonu koymayın.

+0

Mükemmel özlü cevap. – JJS

İlgili konular