Web API Interview Questions and Answers Part 3

1. Does the uri path literals match exactly with template in webapi?
Ans. Yes

2. Does the placeholders matches only specific values by default?
Ans. By default it matches any value unless and until if constraints are not specified.

3. Does the host name of the URI matches with template in webapi?
Ans. No

4. Does the query parameters matches with template in webapi?
Ans. No

5. How the placeholder values are indicated in route template?
Ans. With curly braces

Example : "api/{controller}/public/{category}/{id}"

6. How many special placeholders available in webapi route template?
Ans. There are 2 special placeholders in route template,
1. {controller}
2. {action}

7. Is {action} placeholders is mandatory or not in webapi?
Ans. No, it is not mandatory. It is usually omitted in webapi.

8. What is RouteDictionary?
Ans. After route match, the placeholders are stored in dictionary object with keys as placeholders names
without curly braces and values are taken from URI path or from the defaults.
Generally this dictionary is stored in IHttpRouteData object.

9. What is the use of RouteParameter.Optional in webapi?
Ans. RouteParameter.Optional makes the matching placeholder values not to be stored in dictionary.

Example : 

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{category}/{id}",
    defaults: new { category = "all", id = RouteParameter.Optional }
);

10. Does the defaults values need to be included from routeTemplate path?
Ans. defaults can also include a value that does not contain anywhere in the route template and
if the route matches that value is stored in the dictionary. 

Example:

routes.MapHttpRoute(
    name: "sampleapi",
    routeTemplate: "api/sampleapi/{id}",
    defaults: new { controller = "testapi", id = RouteParameter.Optional }
);






Subscribe

Receive Quality Tutorials Straight in your Inbox by submitting your Email below:

Delivered by FeedBurner

Protected by Copyscape Duplicate Content Checker