Create .NET Web API in Rider
Create .NET Web API
In Rider, go to File > New Solution.
Enter the solution name and select directory. For Target Framework, select .NET 8.0. For Template, select Web API.
There are multiple ways to run the project, at top right, press Run.
Rider has a built-in Terminal accessible at bottom left or using Ctrl + Cmd + 1
Or in Windows/MacOS Terminal, rundotnet run
The browser containing Swagger will open
Create BookController
Right click main folder and add a directory Controllers
.
Right click again to add a class/interface BookController.cs
.
The code should look like this
csharp
using Microsoft.AspNetCore.Mvc;
namespace Library.Controllers;
[ApiController]
[Route("[controller]")]
public class BookController : Controller
{
[HttpGet]
public IActionResult Index()
{
var book = new { Id = 1, Title = "Dotnet" };
return Ok(book);
}
}
Back in Program.cs, add these 2 lines
csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(); //add this
//...
var app = builder.Build();
app.MapControllers(); //add this
//...
In Library.http, change the API to GET /book/
Run the project again and run Library.http to ensure it works.