ASP.NET Web API MCQ questions
Take a short practice test with instant feedback, then revise every question with its answer and the reason behind it.
- Questions
- 123
- From lessons
- 103
- Your last test
- —
Practice test
What does “stateless” mean for a REST API?
All questions with answers
Which base class do Web API 2 controllers inherit from?
Show answer
Answer: B. ApiController
System.Web.Http.ApiController.
Where is Web API routing configured?
Show answer
Answer: B. WebApiConfig.Register
App_Start/WebApiConfig.cs is called from Global.asax.
What is the default Web API route template?
Show answer
Answer: B. api/{controller}/{id}
Actions are chosen by HTTP method, so the template has no {action}.
Which return type lets an action return different HTTP results?
Show answer
Answer: B. IHttpActionResult
Helpers like Ok(), NotFound() and BadRequest() return IHttpActionResult.
Which helper returns 201 with a Location header?
Show answer
Answer: B. CreatedAtRoute("DefaultApi", new { id }, dto)
CreatedAtRoute builds the location from a named route.
By default, where does Web API bind an int parameter from?
Show answer
Answer: B. The URI
Simple types come from the route or query string.
Which attribute forces a simple type to be read from the body?
Show answer
Answer: B. [FromBody]
Only one parameter per action can use [FromBody].
What chooses JSON or XML for the response?
Show answer
Answer: B. Content negotiation using the Accept header
Formatters are chosen from the Accept header and media types.
Which class handles requests before they reach controllers?
Show answer
Answer: B. DelegatingHandler
Message handlers sit at the start of the pipeline.
Which interface implements a global exception logger?
Show answer
Answer: A. IExceptionLogger
IExceptionLogger logs; IExceptionHandler shapes the response.
Which package adds Swagger to Web API 2?
Show answer
Answer: A. Swashbuckle
Swashbuckle (the classic package) generates Swagger for Web API 2.
Which attribute protects an action with bearer tokens?
Show answer
Answer: A. [Authorize]
With OWIN bearer middleware configured, [Authorize] checks the token.
Which call enables CORS attributes in Web API 2?
Show answer
Answer: B. config.EnableCors()
Then apply [EnableCors] to controllers or globally.
What is wrong with this action?
C# Illustrative public IHttpActionResult Get(int id) { var product = _service.GetAsync(id).Result; return Ok(product); }Show answer
Answer: B. It blocks a thread with .Result instead of awaiting
Make the action async and await the call; .Result can deadlock in ASP.NET.
Which ADO.NET object executes a stored procedure?
Show answer
Answer: B. SqlCommand with CommandType.StoredProcedure
Add parameters and call ExecuteReaderAsync or ExecuteNonQueryAsync.
Which EF6 method saves changes asynchronously?
Show answer
Answer: A. SaveChangesAsync()
SaveChangesAsync writes tracked changes in a transaction.
Which setting raises the IIS request size limit?
Show answer
Answer: A. maxAllowedContentLength in requestFiltering
Also raise httpRuntime maxRequestLength (in KB).
Which status code is correct for an unsupported Content-Type?
Show answer
Answer: C. 415
415 Unsupported Media Type; 406 is for an unsatisfiable Accept header.
What is the main reason to migrate to ASP.NET Core?
Show answer
Answer: B. Cross-platform hosting, performance and active development
.NET Framework only receives maintenance; ASP.NET Core is where new features land.
Which pagination response is most helpful?
Show answer
Answer: B. Items plus page, pageSize and totalCount
Metadata lets clients build pagers.
Which base class does a Web API 2 controller inherit?
Show answer
Answer: B. ApiController
Web API 2 controllers inherit System.Web.Http.ApiController.
From the lesson: Introduction to Web APIsWhere is WebApiConfig.Register called from?
Show answer
Answer: B. Global.asax Application_Start
GlobalConfiguration.Configure(WebApiConfig.Register) runs in Application_Start.
From the lesson: Introduction to Web APIsASP.NET Web API 2 runs on Linux without changes.
Show answer
Answer: B. False
It requires .NET Framework, which is Windows-only.
From the lesson: Introduction to Web APIsWhich URL follows REST conventions to delete product 7?
Show answer
Answer: C. DELETE /api/products/7
The resource is in the URL; the action is the HTTP method.
From the lesson: REST PrinciplesWhat does “stateless” mean for a REST API?
Show answer
Answer: B. Each request contains all the information needed to process it
The server keeps no client session between requests.
From the lesson: REST Principles