asp.net-web-api
Snabbstart: Arbeta med JSON
Sök…
Anmärkningar
Exempel för att komma igång snabbt (och korrekt) med ASP.NET WebAPI
Retur JSON från GET med attribut
1. Ställ in din formaterare och routing i Register of ( 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. Skapa metoder i en 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 });
}
exekverar GET /echo/foo
{
"hello": "foo"
}
exekverar GET /echo/1241290805
{
"hello": 1241290805
}
eftersom routningsramverket tar de mest specifika villkoren (datatyp) när du väljer en metod
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow