Thursday, 2 May 2024

Difference between IActionResult and ActionResult in asp.net core

 Please see the below example to understand difference between IActionResult and ActionResult<T>

For Specific type

public Thing Get() {
    return Context.Things.GetThing(1234);
}

This is OK if the action will always return one possible type. However, most actions may return exceptions (i.e. status codes other than 200) that have different types.

IActionResult type

This solves the above problem the IActionResult return type covers different return types.

public IActionResult Get() {
    Thing thing = Context.Things.GetThing(999);
    if (thing == null)

return NotFound(); else return

else return Ok(thing); }

For asynchronous action, use Task<IActionResult>:

public async Task<IActionResult> Get() {
    Thing thing = await Context.Things.GetThing(1234);
    if (thing == null)
        return NotFound();
    else
        return Ok(thing);
}

ActionResult type

ASP.NET Core 2.1 introduced the ActionResult<T> return type which offers the following benefits over the IActionResult type:

1- The action's expected return type is inferred from the T in ActionResult<T>. If you decorate your action with the [ProducesResponseType] attribute, you no longer need to explicitly specify its Type property. For example, you can simply use [ProducesResponseType(200)] instead of [ProducesResponseType(200, Type = typeof(Thing))].

2- T converts to ObjectResult, which means return new ObjectResult(T); is simplified to return T;.

public ActionResult<Thing> Get() {
    Thing thing = Context.Things.GetThing(1234);
    if (thing == null)
        return NotFound();
    else
        return thing;
}

For asynchronous action, use Task<ActionResult<T>>:

public async Task<ActionResult<Thing>> Get() {
    Thing thing = await Context.Things.GetThing(1234);
    if (thing == null)
        return NotFound();
    else
        return thing;
}

Sunday, 10 March 2024

Using Sqlite database with EF Core

 Sqlite is simple file based database; you just need to add reference of below packages in your API project and need to configure path to DB file.

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.Design

Microsoft.EntityFrameworkCore.Sqlite

Microsoft.EntityFrameworkCore.Tools














Once you will create DBContext, you need to create migration and need to update database, below are CLI commands for the same.

1. dotnet ef migrations add Name_Of_Migration

2. dotnet ef database update

Note: No need to create sqlite db file separately once we will update database with migration it will automatically create DB file.





SQLite DB file path in ASP.Net Core application

 I was facing issue to provide file path in EF Core DBContext connection string, after struggling for a while I was successfully able to connect to DB.



How to create and use middleware in asp.net core

Middleware is piece of code that's assembled into an app pipeline to handle requests and responses.  Each middleware component in the re...