Web API


{
  "iq":[{"id":"1",
    "q":"  What is Web API?",
    "answer":"Web API is a framework which is used to build/develop Http based services."
  },
    {"id":"2",
      "q":"What is REST? ",
      "answer":"REST statnds for Representational state transfer.\nIt is a architectural style, which defines rules for creating scalable services.\nREST is works on HTTP protocol using its verbs GET, POST, PUT and DELETE. "
    },
    {"id":"3",
      "q":" Can we do unit test Web API?",
      "answer":"  Web API can be unit test by using Fiddler tool.\nFollowing is the settings to be updated in Fiddler:\nCompose Tab -> Enter Request Headers -> Enter the Request Body and executeon."
    },

    {"id":"4",
      "q":"  Explain Web API Routing?",

      "answer":"Routing is the mechanism of pattern matching as we have in MVC. These routes will get registered in Route Tables. Below is the sample route in Web API –\nRoutes.MapHttpRoute(\nName: \"MyFirstWebAPIRoute\",\nrouteTemplate: “api/{controller}/{id}\ndefaults: new { id = RouteParameter.Optional}}; "
    },
    {"id":"5",
      "q":"List out the differences between WCF and Web API? ",
      "answer":"WCF\nIt is framework build for building or developing service oriented applications.\nWCF can be consumed by clients which can understand XML.\nWCF supports protocols like – HTTP, TCP, Named Pipes etc.\n\nWeb API\nIt is a framework which helps us to build/develop HTTP services\nWeb API is an open source platform.\nIt supports most of the MVC features which keep Web API over WCF."
    },
    {"id":"6",
      "q":" List out differences between MVC and Web API?",
      "answer":"MVC\nMVC is used to create a web app, in which we can build web pages.\nFor JSON it will return JSONResult from action method.\nAll requests are mapped to the respective action methods.\n\nWeb API\nThis is used to create a service using HTTP verbs.\nThis returns XML or JSON to client.\nAll requests are mapped to actions using HTTP verbs.  "
    },

    {"id":"7",
      "q":"What are the advantages of Web API?",
      "answer":" Below are the list of support given by Web API –\nOData\nFilters\nContent Negotiation\nSelf Hosting\nRouting\nModel Bindings "
    },

    {"id":"8",
      "q":"Can we return view from Web API?",
      "answer":"No. We cannot return view from Web API."
    },
    {"id":"9",
      "q":"How we can restrict access to methods with specific HTTP verbs in Web API?",

      "answer":"Attribute programming is used for this functionality. Web API will support to restrict access of calling methods with specific HTTP verbs. We can define HTTP verbs as attribute over method as shown below\n[HttpPost]\npublic void UpdateTestCustomer(Customer c)\n{\nTestCustomerRepository.AddCustomer(c);\n}"
    },

    {"id":"10",
      "q":"Can we use Web API with ASP.NET Web Forms?",
      "answer":"Yes. We can use Web API with ASP.NET Webforms."
    },

    {"id":"11",
      "q":"List out the steps to be made for Web API to work in Web Forms?",

      "answer":"Below are the steps to be followed –\nCreating new controller for Web API.\nAdding routing table to “Application_Start” method in Global.asax\nMake a AJAX call to Web API actions."
    },

    {"id":"12",
      "q":" Explain how to give alias name for action methods in Web API?",
      "answer":"Using attribute “ActionName” we can give alias name for Web API actions. Eg:\n[HttpPost]\n[ActionName(\"AliasTestAction\")]\npublic void UpdateTestCustomer(Customer c)\n{\nTestCustomerRepository.AddCustomer(c);\n }"
    },
    {"id":"13",
      "q":"What is the difference between MVC Routing and Web API Routing?",
      "answer":" There should be atleast one route defined for MVC and Web API to run MVC and Web API application respectively. In Web API pattern we can find “api/” at the beginning which makes it distinct from MVC routing. In Web API routing “action” parameter is not mandatory but it can be a part of routing."
    },

    {"id":"14",
      "q":" Explain Exception Filters?",
      "answer":" Exception filters will be executed whenever controller methods (actions) throws an exception which is unhandled. Exception filters will implement “IExceptionFilter” interface."
    },

    {"id":"15",
      "q":" How can we pass multiple complex types in Web API?",
      "answer":" Below are the methods to pass the complex types in Web API –\nUsing ArrayList\nNewtonsoft JArray "
    },

    {"id":"16",
      "q":" Write a code snippet for passing arraylist in Web API? ",
      "answer":"Below is the code snippet for passing arraylist –\nArrayList paramList = new ArrayList();\nCategory c = new Category { CategoryId = 1, CategoryName = \"SmartPhones\"};\nProduct p = new Product { ProductId = 1, Name = \"Iphone\", Price = 500, CategoryID = 1 };\nparamList.Add(c);\nparamList.Add(p); "
    },

    {"id":"17",
      "q":"Give an example of Web API Routing?",
      "answer":"Below is the sample code snippet to show Web API Routing –\nconfig.Routes.MapHttpRoute(\nname: \"MyRoute\",\nrouteTemplate: \"api/{controller}/{action}/{id}\",\ndefaults: new { id = RouteParameter.Optional });"
    },
    {"id":"18",
      "q":"Give an example of MVC Routing? ",
      "answer":"routes.MapRoute(\nname: \"MyRoute\",\nurl: \"{controller}/{action}/{id}\",\ndefaults: new\n{\ncontroller = \"TrendingdevelopersController\",\naction = \"TrendingdevelopersAction\",\nid = UrlParameter.Optional\n}\n);"
    },

    {"id":"19",
      "q":"How we can handle errors in Web API?",
      "answer":"Below are the list of classes which can be used for error handling -\nHttpResponseException\nException Filters\nRegistering Exception Filters\nHttpError   "
    },

    {"id":"20",
      "q":" Explain how we can handle error from “HttpResponseException”?",
      "answer":"This returns the HTTP status code what you specify in the constructor. Eg :\npublic TestClass MyTestAction(int id)\n{\nTestClass c = repository.Get(id);\nif (c == null)\n{\n throw new HttpResponseException(HttpStatusCode.NotFound);\n}\nreturn c;\n} "
    },
    {"id":"21",
      "q":"How to register Web API exception filters?",
      "answer":" Below are the options to register Web API exception filters –\nFrom Action\nFrom Controller\nGlobal registration   "
    },
    {"id":"22",
      "q":" Write a code snippet to register exception filters from action?",
      "answer":" Below is the code snippet for registering exception filters from action –\n[NotImplExceptionFilter]\npublic TestCustomer GetMyTestCustomer(int custid)\n{\n}   "
    },

    {"id":"23",
      "q":" How to handle error using HttpError?",
      "answer":" HttpError will be used to throw the error info in response body. “CreateErrorResponse” method is used along with this, which is an extension method defined in “HttpRequestMessageExtensions”."
    },{"id":"24",
      "q":" Explain how Web API tracing works? ",
      "answer":"To enable tracing place below code in –“Register” method of WebAPIConfig.cs file.\nconfig.EnableSystemDiagnosticsTracing();"
    },{"id":"25",
      "q":" Explain how Web API tracing works?",
      "answer":"Tracing in Web API done in façade pattern i.e, when tracing for Web API is enabled, Web API will wrap different parts of request pipeline with classes, which performs trace calls."
    },{"id":"26",
      "q":" Can we unit test Web API?",
      "answer":"Yes we can unit test Web API."
    },{"id":"27",
      "q":"  Explain Authentication in Web API?",
      "answer":"Web API authentication will happen in host. In case of IIS it uses Http Modules for authentication or we can write custom Http Modules. When host is used for authentication it used to create principal, which represent security context of the application."
    },{"id":"28",
      "q":" Explain ASP.NET Identity? ",
      "answer":" This is the new membership system for ASP.NET. This allows to add features of login in our application.\nBelow are the list of features supported by ASP.NET Identity in Web API –\nOne ASP.NET Identity System\nPersistence Control"
    },{"id":"29",
      "q":"What are Authentication Filters in Web API? ",
      "answer":"Authentication Filter will let you set the authentication scheme for actions or controllers. So this way our application can support various authentication mechanisms."
    },{"id":"30",
      "q":" How to set the Authentication filters in Web API? ",
      "answer":"Authentication filters can be applied at the controller or action level. Decorate attribute – \"IdentityBasicAuthentication\” over controller where we have to set the authentication filter."
    },{"id":"31",
      "q":"Explain method – “AuthenticateAsync” in Web API? ",
      "answer":"“AuthenticateAsync” method will create “IPrincipal” and will set on request. Below is the sample code snippet for “AuthenticateAsync” –\nTask AuthenticateAsync(\nHttpAuthenticationContext mytestcontext,\nCancellationToken mytestcancellationToken\n)"
    },{"id":"32",
      "q":" How to set the Error Result in Web API?",
      "answer":"\nBelow is the sample code to show how to set error result in Web API –\nHttpResponseMessage myresponse = new HttpResponseMessage(HttpStatusCode.Unauthorized);\nmyresponse.RequestMessage = Request;\nmyresponse.ReasonPhrase = ReasonPhrase;  "
    },{"id":"33",
      "q":"  Explain method – “ChallengeAsync” in Web API?",
      "answer":"“ChallengeAsync” method is used to add authentication challenges to response. Below is the method signature –\nTask ChallengeAsync(\nHttpAuthenticationChallengeContext mytestcontext,\nCancellationToken mytestcancellationToken\n) "
    },{"id":"34",
      "q":" What are media types?",
      "answer":"It is also called MIME, which is used to identify the data . In Html, media types is used to describe message format in the body.  "
    },{"id":"35",
      "q":"List out few media types of HTTP?",
      "answer":"Below are the list of media types –\nImage/Png\nText/HTML\nApplication/Json"
    },{"id":"36",
      "q":" Explain Media Formatters in Web API?",
      "answer":"Media Formatters in Web API can be used to read the CLR object from our HTTP body and Media formatters are also used for writing CLR objects of message body of HTTP.  "
    },{"id":"37",
      "q":" How to serialize read-only properties?",
      "answer":"Read-Only properties can be serialized in Web API by setting the value “true” to the property –\nSerializeReadOnlyTypes of class – DataContractSerializerSettings. "
    },{"id":"38",
      "q":"  How to indent the JSON in web API?",
      "answer":"var mytestjson = GlobalConfiguration.Configuration.Formatters.JsonFormatter;\nmytestjson.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;"
    },{"id":"39",
      "q":" How to JSON serialize anonymous and weakly types objects?",
      "answer":"Using “Newtonsoft.Json.Linq.JObject” we can serialize and deserialize weakly typed objects."
    },{"id":"40",
      "q":" How to write indented XML in Web API?",
      "answer":"To write the indented xml set “Indent” property to true."
    },{"id":"41",
      "q":" How parameter binding works in Web API? ",
      "answer":"If it is simple parameters like – bool,int, double etc. then value will be obtained from the URL.Value read from message body in case of complex types."
    },{"id":"42",
      "q":"  Why to use “FromUri” in Web API?",
      "answer":"In Web API to read complex types from URL we will use “FromUri” attribute to the parameter in action method. Eg:\npublic MyValuesController : ApiControlle\n{\n public HttpResponseMessage Get([FromUri] MyCustomer c) { ... }\n}"
    },{"id":"43",
      "q":"Why to use “FromBody” in Web API?",
      "answer":"  This attribute is used to force Web API to read the simple type from message body. “FromBody” attribute is along with parameter. Eg:\npublic HttpResponseMessage Post([FromBody] int customerid, [FromBody] string customername) { ... }"
    },{"id":"44",
      "q":"Why to use “IValueprovider” interface in Web API?",
      "answer":" This interface is used to implement custom value provider."
    }













  ]

}