Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:15:45+00:00 2026-06-09T15:15:45+00:00

When I return a StatusDescription with a newline using the HttpStatusCodeResult from ASP.Net MVC

  • 0

When I return a StatusDescription with a newline using the HttpStatusCodeResult from ASP.Net MVC 3.0, the connection to my client is forcibly closed. App is hosted in IIS 7.0.

Example controller:

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

Example client:

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

Thrown Exception:

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

The behavior is consistent when using curl (curl 7.25.0 (i386-pc-win32) libcurl/7.25.0 zlib/1.2.6)

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

curl: (56) Recv failure: Connection was reset

Edit

I ended up using this custom ActionResult to get the desired results.

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();
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:15:46+00:00Added an answer on June 9, 2026 at 3:15 pm

    You can’t have a linebreak in the middle of an HTTP header.

    The HTTP protocol specifies the end of a header is a line break.

    Since the line break is in the middle of a header, the header is not a valid header and you are getting this error.

    Fix: Don’t put a line break in the middle of an HTTP header.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application, with some RESTful services that I'm trying to
I have the following models in my ASP.NET MVC 3 project: public class Task
What return type should one return from a method that is getting rows from
Here's the code I'm having trouble with. I'm using an EDMX modeled from an
To return an error from a $.ajax call, there's gotta be a better way
I've created a DLL in C# using the .NET 3.0 framework. Below is the
I have the following linq query from c in AllContracts group c.StatusId by c.StatusDescription
I have a simple class in an ASP.NET webservice. It runs fine on my
I'm serving up an image from a database using an IHttpHandler. The relevant code
I have files that I want to delete. Connection can be from file sharing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.