Tuesday, March 5, 2019

How to implement CRUD features in ASP.NET Web API?

We are going to discuss how we can implement RESTful service layer using ASP.NET Web API.
In this program, we will use session object to persist as in-memory datastore.
Yes, REST is stateless. By using session, we will make it stateful. Well, the only purpose of using session in this program is to use some kind of back-end data store when we do CRUD actions. We are not using this type of data store in production code. We will use industry grade databases for that.
Alright, let's start hands-on work now. Open up Visual Studio 2015 and select File => New => Project and select "ASP.NET Web Application (.NET Framework)". Click "WebAPI" checkbox and create new project.
First we will create the Model class for Employee entity.
Model class
And make sure our WebApi return data in json format.
Configure to return JSON format
Here we will implement the API controller logic
Implement the RESTful API in the controller
In real-word application, we will have back-end database with ORM tool like Entity Framework. We will use dummy data for demo.
Generate the sample data in ActionFilter
For any WebApi, security is a very important factor. For simplicity sake, I just basic authentication to show-case the usage pattern.
For authenticating the routes
Add the entry for authentication in web.config file.
  <system.webServer>    <modules>      <add name="BasicAuthHttpModule" type="WebApiSampleProject.BasicAuthHttpModule, WebApiSampleProject" />    </modules>  </system.webServer>

1 comment:

  1. Implementing CRUD (Create, Read, Update, Delete) features in an application involves setting up corresponding APIs or functions. Sites Proxy Torrenting Utilize frameworks like Django, Express, or Spring Boot to define routes for each operation.

    ReplyDelete

Image compression using C#

Sometimes, we need to compress the image files while maintaining the image quality. This can be achieved by the following C# implementation....