ASP.Net Web API Interview Questions

    1 Votes

46) How to indent the JSON in web API?

Below is the code snippet to make JSON indenting –
var mytestjson = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
mytestjson.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
47) How to JSON serialize anonymous and weakly types objects?

Using “Newtonsoft.Json.Linq.JObject” we can serialize and deserialize weakly typed objects.

48) What is the use of “IgnoreDataMember” in Web API?

By default if the properties are public then those can be serialized and deserialized, if we does not want to serialize the property then decorate the property with this attribute.

49) How to write indented XML in Web API?

To write the indented xml set “Indent” property to true.

50) How to set Per-Type xml serializer?

We can use method – “SetSerializer”. Below is the sample code snippet for using it –

var mytestxml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
// Use XmlSerializer for instances of type "Product":
mytestxml.SetSerializer<Product>(new XmlSerializer(typeof(MyTestCustomer)));

51) What is “Under-Posting” and “Over-Posting” in Web API?

  • Under-Posting” - When client leaves out some of the properties while binding then it’s called under – posting.
  • Over-Posting” – If the client sends more data than expected in binding then it’s called over-posting.

52) How to handle validation errors in Web API?

Web API will not return error to client automatically on validation failure. So its controller’s duty to check the model state and response to that. We can create a custom action filter for handling the same.

53) Give an example of creating custom action filter in Web API?

Below is the sample code for creating custom action filter –

public class MyCustomModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
//Code goes here
}
}
}

In case validation fails here it returns HTTP response which contains validation errors.

54) How to apply custom action filter in WebAPI.config?

Add a new action filter in “Register” method as shown -

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Filters.Add(new MyCustomModelAttribute());
// ...
}
}

55) How to set the custom action filter in action methods in Web API?

Below is the sample code of action with custom action filter –

public class MyCustomerTestController : ApiController
{
[MyCustomModelAttribute]
public HttpResponseMessage Post(MyTestCustomer customer)
{
// ...
}
}

56) What is BSON in Web API?

It’s is a binary serialization format. “BSON” stands for “Binary JSON”. BSON serializes objects to key-value pair as in JSON. Its light weight and its fast in encode/decode.

57) How to enable BSON in server?

Add “BsonMediaTypeFormatter” in WebAPI.config as shown below

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Add(new BsonMediaTypeFormatter());
// Other Web API configuration goes here
}
}

58) How parameter binding works in Web API?

Below are the rules followed by WebAPI before binding parameters –

  • 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.

59) Why to use “FromUri” in Web API?

In Web API to read complex types from URL we will use “FromUri” attribute to the parameter in action method. Eg:

public MyValuesController : ApiController
{
public HttpResponseMessage Get([FromUri] MyCustomer c) { ... }
}

60) Why to use “FromBody” in Web API?

This attribute is used to force Web API to read the simple type from message body. “FromBody” attribute is along with parameter. Eg:

public HttpResponseMessage Post([FromBody] int customerid, [FromBody] string customername) { ... }

61) Why to use “IValueprovider” interface in Web API?

This interface is used to implement custom value provider.

Page 4 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C# Interview Questions
.Net Framework Interview Questions and Answers
ADO.Net Interview Questions and Answers for Freshers, Experienced
ASP.Net Interview Questions and Answers
ASP.Net MVC Interview questions and Answers
WCF Interview Questions and Answers
Top 50 Entity framework interview questions & answers