asp.net-web-api
クイックスタート:JSONを使って作業する
サーチ…
備考
ASP.NET WebAPIを使って素早く(正しく)実行するための例
属性を使用してGETからJSONを返します。
1.( App_Start/WebApiConfig )のRegisterでフォーマッタとルーティングを設定します。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
config.MapHttpAttributeRoutes();
}
}
2. 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 });
}
GET /echo/foo実行する
{
"hello": "foo"
}
GET /echo/1241290805実行する
{
"hello": 1241290805
}
方法を選択するときにルーティングフレームワークが最も特定の条件(データ型)を取るため
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow