Table of Contents
- $.ajax call with header request.
- API controller with added route-prefix, route and AustomAuthFilter attribute.
- Authorization filter attribute.
- get custom header value using request header.
This is the API base url as given below.
var baseURL = "localhost://3456/API/BusinessEntity/GetBusinessEntityDetailByID/1";
The $.ajax call with header request
$.ajax({
url: baseURL,
type: "POST",
data: {},
beforeSend: function (xhr) {
xhr.setRequestHeader("UserType", "admin");
},
async: true,
cache: false,
success: function (data) {
}
});
API Controller with CustomAuthFilter attribute
namespace PCX.API
{
[RoutePrefix("API/BusinessEntity")]
[CustomAuthFilter]
public class BusinessEntityController : BaseAPIController
{
[Route("GetBusinessEntityDetailByID/{costcenterid}")]
public IHttpActionResult GetBusinessEntityDetailByID(int CostCenterID)
{
List<string> lCostCenterName = new BusinessEntityRepository().GetCostCenterWithCodeByID(CostCenterID);
return Json(lCostCenterName);
}
}
}
Get custom header value using request header
namespace PCX.Portal.Api
{
public class CustomAuthFilter : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
var FilePath = System.Web.HttpContext.Current.Request.FilePath;
IEnumerable<string> headerValues =
actionContext.Request.Headers.GetValues("UserType").FirstOrDefault();
}
}
}
}