Today there are no tools built into Visual Studio to test WEB API. Using browsers, one can only test http GET
requests. You need to use third-party tools like Postman, SoapUI, Fiddler or Swagger to perform a complete testing of the WEB API. In ASP.NET Core 2.2, a CLI based new dotnet core global tool named “http-repl” is introduced to interact with API endpoints. It’s a CLI based tool which can list down all the routes and execute all HTTP verbs. In this post, let’s find out how to use HTTP-REPL tool to test WEB API in ASP.NET Core 2.2.
Read More
Tag: Web api
Quick Tip – Return HTTP Status Code from ASP.NET Core Methods
It is advisable to return the proper HTTP status code in response to a client request. This helps the client to understand the request’s result and then take corrective measure to handle it. Proper use of the status codes will help to handle a request’s response in an appropriate way. Out of the box, ASP.NET Core has inbuilt methods for the most common status codes. Like,
return Ok();
// Http status code 200return Created();
// Http status code 201return NoContent();
// Http status code 204return BadRequest();
// Http status code 400return Unauthorized();
// Http status code 401return Forbid();
// Http status code 403return NotFound();
// Http status code 404
Support multiple versions of ASP.NET Core Web API
Versioning helps in rolling out features on a timely basis, without breaking the existing system. It can also help to provide additional functionalities to selected customers. API versioning can be done in different ways like appending the version in the URL or as a query string parameter, via custom header and via Accept-Header. In this post, let’s find how to support multiple version ASP.NET Core Web API. Read More
Import and Export xlsx in ASP.NET Core
This post shows how to import and export .xls or .xlsx (Excel files) in ASP.NET Core. And when thinking about dealing with excel with .NET, we always look for third-party libraries or component. And one of the most popular .net library that reads and writes Excel 2007/2010 files using the Open Office Xml format (xlsx) is EPPlus. However, at the time of writing this post, this library is not updated to support .NET Core. But there exists an unofficial version of this library EPPlus.Core which can do the job of import and export xlsx in ASP.NET Core. This works on Windows, Linux and Mac. Read More
Accept only int value for ASP.NET Web API action method parameter
In this short post, find solution to how to accept only int value for ASP.NET Web API action method parameter. Below is an example of Web API action method which accepts an int type parameter. And it returns the same value as response.
// GET api/values/5 [HttpGet("{id}")] public string Get(int id) { return id.ToString(); }
So when you hit http://localhost:59845/api/values/5
, you get “5” returned in response. And when you hit http://localhost:59845/api/values/abc
, then you get “0” as returned. But what if, you want to accept only integer value and if input parameter is not “int” then return a 404 error.
With attribute routing, you can also define the data type constraint for parameter. So updated action method is,
// GET api/values/5 [HttpGet("{id:int}")] public string Get(int id) { return id.ToString(); }
So now when you hit http://localhost:59845/api/values/abc
, then you shall get 404 error. You can use any data type for route constraint as per your requirement.
Hope you like it. Stay tuned for more!.