WEB API Interview Questions

What is Web Api?
This is the new framework for building HTTP services with easy and simple way.
Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
It is light weight architecture and good for devices which have limited bandwidth like smart phones.

Why you should chooseWeb Api?
Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.

What is Content Negotiation in WEB API?
Content Negotiation is a mechanism that allows a user to decide what kind of response he want to get from the API.
The primary mechanism for content negotiation in HTTP are these request headers:
  1. Accept: Which media types are acceptable for the response, such as "application/json," "application/xml," or a custom media type such as "application/vnd.example+xml"
  2. Accept-Charset: Which character sets are acceptable, such as UTF-8 or ISO 8859-1.
  3. Accept-Encoding: Which content encodings are acceptable, such as gzip.
  4. Accept-Language: The preferred natural language, such as "en-us".
To get JSON response pass "application/json" in Accept Header.
To get XML response pass "application/xml" in Accept Header.

Secure Web API with Token based Authentication
Web-api-token-based-authentication

Exception Handling in ASP.NET Web API
Exception-handling-in-web-api


What is Webapi Filters
Web-api-filters

What is cross-origin requests (CORS) in ASP.NET Web API
Browser security prevents a web page from making AJAX requests to another domain. This restriction is called the same-origin policy, and prevents a malicious site from reading sensitive data from another site. However, sometimes you might want to let other sites call your web API.
Using CORS, a server can explicitly allow some cross-origin requests while rejecting others.

Enable CORS
Now let's enable CORS in the WebService app. First, add the CORS NuGet package. The CORS package requires Web API 2.0 or later.

Install-Package Microsoft.AspNet.WebApi.Cors

Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method:

using System.Web.Http; namespace WebService { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // New code config.EnableCors(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }

Next, add the [EnableCors] attribute to the TestController class:

using System.Net.Http; using System.Web.Http; using System.Web.Http.Cors; namespace WebService.Controllers { [EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")] public class TestController : ApiController { // Controller methods not shown... } }


0 comments:

Post a Comment

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)