Posts

Showing posts from February, 2021

Startup Class in ASP.NET Core

Image
The startup class is the heart of the ASP.NET Core. It helps to configure the application. By default, it has three modes: Startup constructor, ConfigureService Method, and Configure Method. The following is a default MVC Startup class. Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get ; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler

In-Memory Cache in ASP.NET Core

Image
You can improve the performance of your application by using cache. Instead of generating the content, you can retrieve the content from the cache. You can use cache for rarely changing content. Instead of creating content, the cache will hold the content, so it will be available quickly. ASP.NET Core support IMemoryCache, its cache storage in the memory of the webserver. Let's see how this works. The following is a HomeController code. Here the IMemoryCache interface is implemented using dependency injection. You can see in the controller constructor IMemoryCache object has been passed. In coding, the TryGetValue method checks if there is any cached content for the keyword ‘CacheDateKey’, which will provide content if there is any cached content in it. If there is no content in the cache. It will set the cache, i.e. the time for the cache. Also, the SetSlidingExpiration option is set in the example. This indicates how long the cache should be idle. If any user accesses

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