31) How to enable tracing in Web API?
config.EnableSystemDiagnosticsTracing();
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.
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.
35) Explain ASP.NET Identity?
This is the new membership system for ASP.NET. This allows to add features of login in our application.
Below are the list of features supported by ASP.NET Identity in Web API –
- One ASP.NET Identity System
- Persistence Control
36) What are Authentication Filters in Web API?
Authentication Filter will let you set the authentication scheme for actions or controllers. So this way our application can support various authentication mechanisms.
37) How to set the Authentication filters in Web API?
Authentication filters can be applied at the controller or action level. Decorate attribute – "IdentityBasicAuthentication” over controller where we have to set the authentication filter.
38) Explain method – “AuthenticateAsync” in Web API?
“AuthenticateAsync” method will create “IPrincipal” and will set on request. Below is the sample code snippet for “AuthenticateAsync” –
Task AuthenticateAsync(
HttpAuthenticationContext mytestcontext,
CancellationToken mytestcancellationToken
)
39) How to set the Error Result in Web API?
Below is the sample code to show how to set error result in Web API –
HttpResponseMessage myresponse = new HttpResponseMessage(HttpStatusCode.Unauthorized);
myresponse.RequestMessage = Request;
myresponse.ReasonPhrase = ReasonPhrase;
40) Explain method – “ChallengeAsync” in Web API?
“ChallengeAsync” method is used to add authentication challenges to response. Below is the method signature –
Task ChallengeAsync(
HttpAuthenticationChallengeContext mytestcontext,
CancellationToken mytestcancellationToken
)
41) What are media types?
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.
42) List out few media types of HTTP?
Below are the list of media types –
- Image/Png
- Text/HTML
- Application/Json
43) Explain Media Formatters in Web API?
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.
44) How to serialize read-only properties?
Read-Only properties can be serialized in Web API by setting the value “true” to the property –
“SerializeReadOnlyTypes” of class – “DataContractSerializerSettings”.
45) How to get Microsoft JSON date format ?
Use “DateFormatHandling” property in serializer settings as below –
var myjson = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
myjson.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;