MVC Controller return a bad request?

C#asp.net Mvcasp.net Mvc-4Bad Request

C# Problem Overview


I was wondering if it was possible to return a bad request with content from an MVC Controller? The only way I have been able to do this is to throw HttpException however here I can't set any content. Tried this approach to but for some odd reason I am always getting an OK back. Is it possible to do this?

public class SomeController : Controller
{
	[HttpPost]
    public async Task<HttpResponseMessage> Foo()
	{
		var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
		response.Content = new StringContent("Naughty");

		return response;	
	}
}

C# Solutions


Solution 1 - C#

return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "naughty");

Solution 2 - C#

Set the Http status code to bad request and use Content method to send your content along with response.

public class SomeController : Controller
{
    [HttpPost]
    public async Task<ActionResult> Foo()
    {
        Response.StatusCode = 400;
        return Content("Naughty");
    }
}

Solution 3 - C#

In addition to the @Ekk's answer, make sure to check this:

https://stackoverflow.com/questions/15532667/asp-netazure-400-bad-request-doesnt-return-json-data/15532685#15532685

> Add the following entry to your 'web.config'. > > > > > ...

Solution 4 - C#

Of course you can.

Take a look at my Action

// GET: Student/Details/5
public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Student student = db.Students.Find(id);
    if (student == null)
    {
        return HttpNotFound();
    }
    return View(student);
}

I think this is best practice

  1. to return HttpStatusCodeResult(HttpStatusCode.BadRequest); in case user does not provided a required value

  2. to return HttpNotFound(); in case the user provided a required value but not veiled

hope this help you

Solution 5 - C#

You can pass in error message to the second parameter like so:

return new HttpResponseMessage(HttpStatusCode.BadRequest, "Your message here");

Solution 6 - C#

The TrySkipIisCustomErrors flag can be used to turn off IIS custom error handling.

[HttpGet]
public void Foo()
{
  HttpContext.Response.TrySkipIisCustomErrors = true;
  HttpContext.Response.StatusCode = 400;
  HttpContext.Response.Write("Naughty");
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDr SchizoView Question on Stackoverflow
Solution 1 - C#IanView Answer on Stackoverflow
Solution 2 - C#ShankarSangoliView Answer on Stackoverflow
Solution 3 - C#Masood KhaariView Answer on Stackoverflow
Solution 4 - C#Basheer AL-MOMANIView Answer on Stackoverflow
Solution 5 - C#EkkView Answer on Stackoverflow
Solution 6 - C#Евгений ШироковView Answer on Stackoverflow