Category: MVC

IActionResult Return Types and StatusCodes quick reference

I’m always forgetting which return types are available directly from ASP.Net controllers, so have created a quick listing here.

For general information on HTTP Status codes see
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

ASP.Net Core 2.2

All Status Codes:

https://docs.microsoft.com/en-gb/dotnet/api/microsoft.aspnetcore.http.statuscodes?view=aspnetcore-2.2

ControllerBase IActionResult Return Types

  • OK (200)
  • BadRequest (400)
  • Forbid (403)
  • LocalRedirect (302)
  • LocalRedirectPermanent (301)
  • LocalRedirectPermanentPreserve (308)
  • LocalRedirectPermanentPreserveMethod (307)
  • NoContent (204)
  • NotFound (404)
  • RedirectToAction (302)
  • RedirectToActionPermanent (301)
  • RedirectToActionPermanentPreserve (308)
  • RedirectToActionPermanentPreserveMethod (307)
  • RedirectToPage (302)
  • RedirectToPagePermanent (301)
  • RedirectToPagePermanentPreserve (308)
  • RedirectToPagePermanentPreserveMethod (307)
  • RedirectToPage (302)
  • RedirectToPagePermanent (301)
  • RedirectToPagePermanentPreserve (308)
  • RedirectToPagePermanentPreserveMethod (307)
  • StatusCode (set own status code)
    • Use 409 – Conflict, for updates that fail due to conflicts such as already exists etc..
  • Unauthorized (401)
  • UnprocessableEntity (422)
  • ValidationProblem (400)

ApiController MVC Compatibility Shim

https://docs.microsoft.com/en-us/dotnet/api/system.web.http.apicontroller?view=aspnetcore-2.2

  • OK (200)
  • BadRequest (400)
  • Conflict (409)
  • Created (201)
  • CreatedAtRoute (201)
  • InternalServerError (500)
  • NotFound (404)
  • Redirect (302)
  • RedirectToRoute (302)
  • StatusCode (set own status code)
    • Use 409 – Conflict, for updates that fail due to conflicts such as already exists etc..
  • Unauthorized (401)
  • UnprocessableEntity (422)
  • ValidationProblem (400)

.Net MVC 3 Logon does not redirect out the box–quick fix!

It seems that the templates for MVC 3 razor are not quite complete. If you attempt to browse to a page where authentication is required, then you will be redirect to the Logon page with a ReturnUrl query string.

Something like:

http://mysite.com/Account/LogOn?ReturnUrl=%2fThePageIWant

That’s great except, out the box, the Logon page does not make use of this and the destination page is not redirected to after a successful login. The fix is easy.

Add the returnUrl route value into the Html.BeginForm method as shown below.

Html.BeginForm("LogOn", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] })

You should already have some code in the controller that does some sanity checking on the returnUrl before doing the redirect. Somthing like:

if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
       && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
    return Redirect(returnUrl);
}

Make sure this is after the SetAuthCookie line!

Redirecting MVC controller to HTTPS if required

Check out this excellent blog on how to create a simple ActionFilterAttribute that handles redirecting to https:// if required.

http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx

then just decorate your whole Controller ot ActionResult method with

[RequiresSSL]
 public class AccountController : Controller
 {
 ...
 }