Thursday, August 1, 2019

ASP.NET Web API

  • What is Web API?
    • It is a framework which helps us to build/develop HTTP services. So there will a client server communication using HTTP protocol.
  • What is advantages of REST in Web API?
    • REST always used to make less data transfers between client and server which makes REST an ideal for using it in mobile apps. Web API supports HTTP protocol thereby it reintroduces the old way of HTTP verbs(GET, POST, PUT and DELETE) for communication
  • What is REST?
    • REpresentational State Transfer 
    • Architectural style, which has defined guidelines for creating services which are scalable
  • Explain Web API Routing
    • Routing is the mechanism of patten matching
    • Global.asax - Application_Start() event - GlobalConfiguration.Configure(WebApiConfig.Register);
    • In WebApiConfig.cs - config.MapHttpAttributeRoutes();
    • HttpConfiguration config.Routes.MapHttpRoute(
                      name: "DefaultApi",
                      routeTemplate: "api/{controller}/{id}",
                      defaults: new { id = RouteParameter.Optional }
                  );
  • MVC Routing
    •  Global.asax - Application_Start() event - RouteConfig.RegisterRoutes(RouteTable.Routes);
    • In RouteConfig.cs - routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    • RouteCollection routes.MapRoute(
                      name: "Default",
                      url: "{controller}/{action}/{id}",
                      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                  );
  • What are the advantages of Web API
    • OData - Open Data Protocol is a data access protocol for web
    • Filters
    • Content Negotiation
    • Self Hosting
    • Routing
    • Model Bindings
  •  How to give alias name for Action methods in Web API
    • Using Attribute ActionName["AliasName"] above Action Method
  • Explain Exception Filters
    • Exception filters will be executed whenever controller methods throws exception which is unhandles
    • Exception filter will implement "IExceptionFilter" interface
  • New features added in Web API 2.0
    • OWIN - Open web interface in .NET
    • Attribute Routing
    • External Authentication
    • Web API OData
  • How we can handle exception in Web API
    • HttpResponseException
    • Exception Filters - Inherit ExceptionFilterAttribute  to create customExceptionFilter
    • Register Exception Filter
    • HttpError - Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
  • Register Exception filters globally
    • GlobalConfiguration.Configuration.Filters.Add(new CustomerExceptionFilterAttribute());
  • How to enable tracing in WebAPI
    • config.EnableSystemDiagnosticsTracing();
  • What are Authentication Filters in Web API
    • let you set the authentication scheme for actions or controllers
  • AuthenticateAsync
  • ChanllengeAsyc
  • Media Formatters
    • Media type formatters are classes responsible for serializing request/response data so that Web API can understand the request data format and send data in the format which client expects.
    • JsonMediaTypeFormatter 
    • XmlMediaTypeFormatter
    • FormUrlEncodedMediaTypeFormatter
    • JQueryMvcFormUrlEncodedFormatter
  • Custom Action Filter
    • public class MyCustomModelAttribute : ActionFilterAttribute
      {
          public override void OnActionExecuting(HttpActionContext actionContext)
          {
          }
      } 
  • WebAPI Return types
    • void
      HttpResponseMessage
      IHttpActionResult
      Objects or Lists of Objects
       

No comments:

Post a Comment