asp.net-web-api
Inicio rápido: Trabajar con JSON
Buscar..
Observaciones
Ejemplos para ponerlo en marcha rápidamente (y correctamente) con ASP.NET WebAPI
Devuelve JSON desde GET usando atributos
1. Configure su formateador y enrutamiento en el Register de ( App_Start/WebApiConfig )
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
config.MapHttpAttributeRoutes();
}
}
2. Crear métodos en un ApiController
public class HelloWorldController : ApiController
{
[HttpGet]
[Route("echo/{message}")]
public IHttpActionResult Echo(string message) {
return Ok(new{ hello: message });
}
[HttpGet]
[Route("echo/{digits:int}")]
public IHttpActionResult Echo(int digits) {
return Ok(new{ hello: digits });
}
ejecutando GET /echo/foo
{
"hello": "foo"
}
ejecutando GET /echo/1241290805
{
"hello": 1241290805
}
A medida que el marco de enrutamiento toma las condiciones más específicas (tipo de datos) al elegir un método
Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow