Published: January 20 2023

.NET 7.0 - Create a Base Controller in .NET

Tutorial built with .NET 7.0

This is a super quick post on how to create base controller in .NET.

A base controller is useful for holding common properties and/or functionality that you want all controllers to inherit.

The code snippets in this example are from a .NET 7.0 tutorial I posted recently that supports login with Facebook, the full tutorial and project code are available at .NET 7.0 - Facebook Authentication API Tutorial with Example.


.NET Base Controller

Path: /Controllers/BaseController.cs

The base controller is inherited by all other controllers in the API and includes common properties and methods that are accessible to all controllers.

The Account property returns the current authenticated account for the request from the HttpContext.Items collection, or returns null if the request is not authenticated. The current account is added to the HttpContext.Items collection by custom JWT middleware when the request contains a valid JWT token in the authorization header.

namespace WebApi.Controllers;

using Microsoft.AspNetCore.Mvc;
using WebApi.Entities;

[Controller]
public abstract class BaseController : ControllerBase
{
    // returns the current authenticated account (null if not logged in)
    public Account? Account => (Account?)HttpContext.Items["Account"];
}
 

Accounts Controller that inherits from the Base Controller

Path: /Controllers/AccountsController.cs

The accounts controller defines and handles all routes / endpoints for the API that relate to accounts, it inherits from the BaseController so has direct access to the Account property, for example the account is returned from the GetCurrent() action method.

namespace WebApi.Controllers;

using Microsoft.AspNetCore.Mvc;
using WebApi.Authorization;
using WebApi.Models.Accounts;
using WebApi.Services;

[ApiController]
[Authorize]
[Route("[controller]")]
public class AccountsController : BaseController
{
    private IAccountService _accountService;

    public AccountsController(IAccountService accountService)
    {
        _accountService = accountService;
    }

    [AllowAnonymous]
    [HttpPost("authenticate")]
    public async Task<IActionResult> Authenticate(AuthenticateRequest model)
    {
        var response = await _accountService.Authenticate(model);
        return Ok(response);
    }

    [HttpGet("current")]
    public IActionResult GetCurrent()
    {
        return Ok(Account);
    }

    [HttpPut("current")]
    public async Task<IActionResult> UpdateCurrent(UpdateRequest model)
    {
        var account = await _accountService.Update(Account.Id, model);
        return Ok(account);
    }

    [HttpDelete("current")]
    public async Task<IActionResult> DeleteCurrent()
    {
        await _accountService.Delete(Account.Id);
        return Ok();
    }
}

 


Need Some .NET Help?

Search fiverr for freelance .NET developers.


Follow me for updates

On Twitter or RSS.


When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Comments


Supported by