Skip to content

Instantly share code, notes, and snippets.

@dapperdandev
Last active March 19, 2017 16:46
Show Gist options
  • Save dapperdandev/01e90696088a1b61892f6fcb3874c7b5 to your computer and use it in GitHub Desktop.
Save dapperdandev/01e90696088a1b61892f6fcb3874c7b5 to your computer and use it in GitHub Desktop.
ASP.NET Web API

ASP.NET Web API

Action Results, Error Handling, & Validation

Error Handling

Check if null

if (foo == null)
{
  return NotFound();
}

Check if save successful

if (newFoo == null)
{
  return Conflict();
}

Exception Handling (500)

try
{
   // Do something
}
catch (Exception ex)
{
  return InternalServerError(ex);
}

Validation

Use validation attributes in the model with DataAnnotations using System.ComponentModel.DataAnnotations;

[Required]
[MinLength(5)]
[MaxLength(10, ErrorMessage = "Foo max length is 10 characters")]
public string Foo {get; set; }

Authentication

Important Files

IdentifyConfig.cs: used to configure validation requirements
Startup.Auth.cs: used to configure authorization type (facebook, twitter, https/http, etc.)

// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = false

User Registration and Login

CORS is a pain in the butt

User Authorization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment