The following code adds all the dependencies required by MVC 6 and is automatically invoked at startup ConfigureServices
.
- Add the following code to the configuration method to
UseMvc
add MVC 6 to the pipeline.
public void Configure (Iapplicationbuilder app)
{
New:
App. Usemvc ();
}
The following is the complete Startup
class code:
using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
New using:
using Microsoft.Framework.DependencyInjection;
namespace Todoapi
{
Public class Startup
{
Add This method:
public void configureservices (iservicecollection services)
{
Services. Addmvc ();
}
public void Configure (Iapplicationbuilder app)
{
New:
App. Usemvc ();
App. Usewelcomepage ();
}
}
}
Add Modelmodel represents the data field of the application. In this example, a ToDo item is stored in model. Add the following classes to the project:
using System.ComponentModel.DataAnnotations;
namespace Todoapi.models
{
Public class TodoItem
{
Public Get set; }
[Required]
Public string Get set; }
Public Get set; }
}
}
To keep the project neat, I created the Models folder for the Model class, which is not a necessary operation, of course.
Add ControllerAdd a controller class to handle HTTP requests. Add the following classes to the project:
using MICROSOFT.ASPNET.MVC;
using System.Collections.Generic;
using System.Linq;
using Todoapi.models;
namespace Todoapi.controllers
{
[Route ("Api/[controller]")]
Public class Todocontroller:controller
{
Static ReadOnly New List<todoitem> ()
{
New TodoItem {Id = 1, Title = "FirstItem"}
};
[HttpGet]
Public Ienumerable<todoitem> GetAll ()
{
return _items;
}
[HttpGet ("{id:int}", Name = "getbyidroute")]
Public Iactionresult GetById (int id)
{
var item = _items. FirstOrDefault (x = x.id = = Id);
if (item = = NULL)
{
return Httpnotfound ();
}
return New Objectresult (item);
}
[HttpPost]
public void Createtodoitem ([frombody] TodoItem Item)
{
if (! Modelstate.isvalid)
{
Context.Response.StatusCode = 400;
}
Else
{
Item. Id = 1+ _items. Max (x = (int?) x.id)?? 0;
_items. ADD (item);
string url = Url.routeurl ("getbyidrouteNew
Request.scheme, Request.Host.ToUriComponent ());
Context.Response.StatusCode = 201;
context.response.headers["location"] = URL;
}
}
[Httpdelete ("{ID}")]
Public Iactionresult DeleteItem (int id)
{
var item = _items. FirstOrDefault (x = x.id = = Id);
if (item = = NULL)
{
return Httpnotfound ();
}
_items. Remove (item);
return New Httpstatuscoderesult (204); 201 No Content
}
}
}
Similarly, I created the Controllers folder to store the controller.
In the subsequent chapters we will further elaborate on the Controller's code. The following are some of the basic functions that the controller implements:
For example, here's an HTTP request to get a ToDo project:
GET Http://localhost:5000/api/todo http/1.1
User-agent:fiddler
host:localhost:5000
Here is the response stream:
http/1.1 OK
Content-type:application/json;charset=utf-8
server:microsoft-httpapi/2.0
Date: Thu, Oct 22:40:31 GMT
content-length:46
[{"Id": 1, "Title": "FirstItem", "IsDone":false}]
In the following sections we will explain: