.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
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
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();
}
}
Subscribe or Follow Me For Updates
Subscribe to my YouTube channel or follow me on Twitter, Facebook or GitHub to be notified when I post new content.
- Subscribe on YouTube at https://www.youtube.com/JasonWatmore
- Follow me on Twitter at https://twitter.com/jason_watmore
- Follow me on Facebook at https://www.facebook.com/JasonWatmoreBlog
- Follow me on GitHub at https://github.com/cornflourblue
- Feed formats available: RSS, Atom, JSON
Other than coding...
I'm currently attempting to travel around Australia by motorcycle with my wife Tina on a pair of Royal Enfield Himalayans. You can follow our adventures on YouTube, Instagram and Facebook.
- Subscribe on YouTube at https://www.youtube.com/TinaAndJason
- Follow us on Instagram at https://www.instagram.com/tinaandjason
- Follow us on Facebook at https://www.facebook.com/TinaAndJasonVlog
- Visit our website at https://tinaandjason.com.au
Need Some .NET Help?
Search fiverr to find help quickly from experienced .NET developers.