Build Web API using Angular with ASP.NET MVC 5 (Part 1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Http;
namespace Angular_mvc5
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Angular_mvc5.Models
{
public class Comment
{
public int id { get; set; }
public string content { get; set; }
public int parent { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Angular_mvc5.Models;
namespace Angular_mvc5.Controllers
{
public class CommentController : ApiController
{
// GET: api/Comment
List<Comment> _cmt = new List<Comment>();
public CommentController()
{
_cmt.Add(new Comment() { id = 1, content = "Cmt A", parent = 0 });
_cmt.Add(new Comment() { id = 2, content = "reply Cmt A", parent = 1 });
_cmt.Add(new Comment() { id = 3, content = "reply Cmt A", parent = 2 });
_cmt.Add(new Comment() { id = 4, content = "Cmt B", parent = 0 });
_cmt.Add(new Comment() { id = 5, content = "reply Cmt B", parent = 4 });
} public IHttpActionResult Get()
{
var data = _cmt.ToList();
return Ok(data);
}
// GET: api/Comment/5
public IHttpActionResult Get(int id)
{
var data = _cmt.Where(s => s.id == id).ToList();
return Ok(data);
}
// POST: api/Comment
public IHttpActionResult Post(Comment _data)
{
_cmt.Add(_data);
var result = _cmt.ToList();
return Ok(result);
}
// PUT: api/Comment/5
public IHttpActionResult Put(int id, Comment _data)
{
_cmt.Where(s => s.id == id).ToList().ForEach(s => s.content =_data.content);
return Ok(_cmt.ToList());
}
// DELETE: api/Comment/5
public IHttpActionResult Delete(int id)
{
_cmt.Remove(_cmt.FirstOrDefault(s => s.id == id));
return Ok(_cmt.ToList());
}
}
}

--

--

--

Website:https://hoanguyenit.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The most confusing thing in JavaScript: The ‘this’ keyword

Cache Handling Using Service Workers and the Cache API

Vue.js + Django + MariaDB + nginx with Docker -3 Settings for Vue.js

Why Should You Use a React Component Library for your Project?

useMemo why, when & how

useMemo code snippet

Could Blitz.js be the next big JS framework?

React Native Development: How is it Different for Android and iOS?

Redux State Management With Types

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hòa Nguyễn Thanh

Hòa Nguyễn Thanh

Website:https://hoanguyenit.com

More from Medium

Google Authentication in Angular with OAuth & angularx-social-login

How to Create an Angular Barcode QR Code Scanning Library

Application Structure and Best Practices Of Angular — Part 2

Import and export data in CSV in Angular.