It supports the following return types:
HttpResponseMessage
IHttpActionResult
Void
Other types such as string, int, etc
It supports the following return types:
HttpResponseMessage
IHttpActionResult
Void
Other types such as string, int, etc
One can register exception filter globally using following code:
Web API global filters are registered through the HttpConfiguration object available to you in the Register method WebApiConfig.cs if you're using a project template with WebActivator:
public static void Register(HttpConfiguration config)
{
//stuff before
config.Filters.Add(new MyWebApiFilter());
//stuff after
}
or otherwise in the global.asax.cs:
GlobalConfiguration.Configuration.Filters.Add(new MyWebApiFilter());
One can use HttpResponseException, HttpError, Exception filters, register exception filters, Exception handlers to handle errors. Exception filter can be used to identify unhandled exceptions on actions or controllers, exception handlers can be used to identify any type of unhandled exception application-wide, and HttpResponseException can be used when there is the possibility of an exception.
In web API, media type formatters are classes that are responsible for serialization data.
Web API can understand request data format in a better way and send data in a format that the client expects. It simply specifies data that is being transferred among client and server in HTTP response or request.
JsonMediaTypeFormatter ->application/json, text/json
XmlMediaTypeFormatter ->application/xml, text/json
Steps to Consume Web API in MVC.
Step1: Install HTTP Client library from NuGet.
What is HttpClient?
HttpClient is base class which is responsible to send HTTP request and receive HTTP response resources i.e from REST services.
Step2:Install WebAPI.Client library from NuGet
Step3: Add Model and Controller class.
Step4:
Our hosted Web API REST Service includes these two methods, as given below.
GetAllEmployees (GET )
GetEmployeeById (POST ) which takes id as input parameter
We are going to call GetAllEmployees method which returns the all employee details ,The hosted web api REST service base URL is http://192.168.95.1:5555/ and to call GetAllEmployees from hosted web API REST service, The URL should be Base url+api+apicontroller name +web api method name as following,
http://192.168.95.1:5555/api/Employee/GetAllEmployees
In the preceding url
http://localhost:56290 Is the base address of web API service, It can be different as per your server.
api It is the used to differentiate between Web API controller and MVC controller request .
Employee This is the Web API controller name.
GetAllEmployees This is the Web API method which returns the all employee list.
using ConsumingWebAapiRESTinMVC.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace ConsumingWebAapiRESTinMVC.Controllers
{
public class HomeController : Controller
{
//Hosted web API REST Service base url
string Baseurl = "http://192.168.95.1:5555/";
public async Task<ActionResult> Index()
{
List<Employee> EmpInfo = new List<Employee>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("api/Employee/GetAllEmployees");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
EmpInfo = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse);
}
//returning the employee list to view
return View(EmpInfo);
}
}
}
}What is JWT Token?
JWT stands for Jason Web Token.
Token-based security is commonly used in today’s security architecture. There are several token-based security techniques. JWT is one of the more popular techniques. JWT token is used to identify authorized users.
What is the JWT WEB TOKEN?
Open Standard: Means anywhere, anytime, and anyone can use JWT.
Secure data transfer between any two bodies, any two users, any two servers.
It is digitally signed: Information is verified and trusted.
There is no alteration of data.
Compact: because JWT can be sent via URL, post request & HTTP header.
Fast transmission makes JWT more usable.
Self Contained: because JWT itself holds user information.
It avoids querying the database more than once after a user is logged in and has been verified.
JWT is useful for:
Authentication
Secure data transfer
JWT Token Structure
A JWT token contains a Header, a Payload, and a Signature.
Header
Payload
Signature
{ base64urlencoded (header) +”.”+ base64urlencoded (payload) +”.”+ secret }
Implement the new feature without impacting the existing consumers we can solve this problem by API versioning. When the business has start...