Skip to content

Instantly share code, notes, and snippets.

@sakapon
Created March 22, 2018 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/7641318950b9e61a4537ecc9feff397b to your computer and use it in GitHub Desktop.
Save sakapon/7641318950b9e61a4537ecc9feff397b to your computer and use it in GitHub Desktop.
AspNetWebApiSample/SampleWebApi/Controllers/ValuesController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace SampleWebApi.Controllers
{
public class ValuesController : ApiController
{
static readonly List<string> values = new List<string> { "value0", "value1" };
public string Get(int id)
{
if (id < 0 || values.Count <= id)
{
var message = $"No value with ID = {id}";
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
// No content:
//throw new HttpResponseException(HttpStatusCode.NotFound);
}
return values[id];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment