DEV Community

Cover image for Your first ASP.NET App: Fixes
Сабака Чабака
Сабака Чабака

Posted on

Your first ASP.NET App: Fixes

Hello! In this tutorial we will fix your own ASP.NET App!

Requirements

  • IDE like Visual Studio/JetBrains Rider
  • .NET >= 10
  • Base C# skill
  • CPU (manually)

Fixing Controller

First we need to fix our controller, by removing redundant methods and replacing DateTime to Stopwatch.

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;

namespace ExampleProject;

[Route("[controller]")]
[ApiController]
public class MyController(IMyService service) : ControllerBase
{
    [HttpGet("/{name}")]
    public async Task<IActionResult> Get(string name)
    {
        var start = Stopwatch.GetTimestamp();
        var hello = await service.GetHello(name);
        return Ok($"{hello} It took {Stopwatch.GetElapsedTime(start)} to respond.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Attention!

When you using primary constructor, injectioning service imperceptibly becomes class field, so if Sigleton class depends on Scoped service that scoped service will life all time what's life a singleton.

In next series we learn EF Core

Top comments (0)