The following code adds all the dependencies required by MVC 6 and is automatically invoked at startup ConfigureServices
.
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 int Id {get; set;}
[Required]
public string Title {get; set;}
public bool IsDone {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 list<todoitem> _items = new List<todoitem> ()
{
New TodoItem {Id = 1, Title = "First Item"}
};
[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 ("Getbyidroute", new {id = Item. Id},
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:
650) this.width=650; "Width=" 610 "height=" 241 "title=" image "style=" border-width:0px; "alt=" image "src=" http:/ Images.cnitblog.com/blog/139239/201502/050938002963700.png "border=" 0 "/>
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": "First Item", "IsDone": false}]
In the following sections we will explain: