Posts

Showing posts with the label API

Cross-Origin Requests (CORS) in ASP.NET Core Web API

Image
This blog is going to explain what a Cross Origin Request (CORS) is and how to implement it in ASP.NET Core web API. When a web page tries to access resources from a different webpage it is blocked by the browser security feature. This feature is called the Same Origin Policy (SOP). In many cases the developer has to access resources of different origins. In such cases CORS helps to relax the Same Origin Policy. There are 3 ways to enable CORS in ASP.NET core. Middleware (name policy or default policy) Endpoint routing [EnableCORS] attribute Let’s see that with the example. Imagine you have an API which returns some JSON values. It’s URL is https://localhost:44351/api/Home. When trying to access another domain URL(https: // localhost: 44389 /) from the client page, it will give you the following error message. Access to XMLHttpRequest at 'https://localhost:44351/api/Home' from origin 'https://localhost:44389' has be...

ASP.NET Core Web API Documentation with Swagger

Image
This blog is going to explain how to implement Swagger documentation in ASP.NET Core. Swagger helps to describe API documents so that consumers can easily understand the API. Swagger UI offers a web-based UI. For the developer, this supports creating quick documentation. All public action methods can be tested using the Swagger UI. Step 1: First, you need to create an API project. New Project -> ASP.NET Core web application -> Provide Project Name-> Click Create button-> Select API -> Click Create Button. After that Create a API controller Empty. Step 2: The next step is to install the Swashbuckle package from the Nuget package. If you are using the NuGet UI, search for Swashbuckle.AspNetCore and install it. Otherwise, use the command below if you are using the Package Manager console. Install-Package Swashbuckle.AspNetCore Step 3: The following is a Movie Model. I have added a summary on top of each property to describe the property Movie.cs pub...