LoginSignup
4
8

More than 5 years have passed since last update.

ASP.NET API

Last updated at Posted at 2018-05-19

API..

sinatraとかflaskの手軽さに感動したけど、
久しぶりにやったらASP.NET APIもわるくなかった。

※プロジェクトの用意はここを参考にすると最短で必要十分。
https://qiita.com/ledsun/items/e8bd4acf796a3ff83db3

ReactToDoListController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;
using ReactToDoList.Models;

namespace ReactToDoList.Controllers
{

    public class ReactToDoListController : ApiController
    {
        [HttpGet]
        public IHttpActionResult GetList()
        {
            //さすがにハイジャックはされたくないのでむき出しの配列では返さない。
            return Json(new ListModel {UserList = new List<ToDo>             {
                new ToDo { Title = "パフォーマンス改善", Content = "検索画面が遅いのでいろいろ見てみる。"},
                new ToDo { Title = "この間のキャンペーン", Content = "開発でテスト後本番に反映。"},
                new ToDo { Title = "新機能", Content = "統計データをグラフで表示したい。"}
            }}); 
        }
    }
}
ListModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ReactToDoList.Models
{
    [Serializable]
    public class ListModel
    {
        public List<ToDo> UserList { get; set; }
    }

    public class ToDo
    {
        public string Title { get; set; }
        public string Content { get; set; }
    }
}
WebApiConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace ReactToDoList
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API の設定およびサービス

            // Web API ルート
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}", //ここは編集する。
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

ここからさらにDBと連携まではASP.NET API 2

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8