C
C#9mo ago
wwww

✅ Controller routing in 2023

Hi, as I understand it, just like everything in programming, you can do one thing in many different ways. This also works to controller routing. There are lot of ways to route your controllers in a web app. What is the most common way to route them in 2023? Setting each method from Program.cs? route attribute? maybe just with method names?
21 Replies
Shinigami
Shinigami9mo ago
Hi, afaik you create a controllers folder and create controller in their either by keeping controller attribute over the class name or the class name ending with Controller and in program.cs you just add builder.services.AddControllers() and app.MapControllers.
wwww
wwww9mo ago
i thing we talk about different things let me explain it again, what i meant so, i can
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Shinigami
Shinigami9mo ago
Aahh! Apologies I usually use route attributes
wwww
wwww9mo ago
So, here if i go to "/" it will load home/index
Shinigami
Shinigami9mo ago
[Route("/")]
wwww
wwww9mo ago
also i can set up it like
No description
wwww
wwww9mo ago
or like this
No description
Shinigami
Shinigami9mo ago
Yup, right!
wwww
wwww9mo ago
atleast 3 ways and i belive there is more ways to do that
Shinigami
Shinigami9mo ago
I feel route attributes are cleaner, since you wouldn't clutter program.cs
wwww
wwww9mo ago
and im asking, which is most used in our time so, in case im using attributes, i don't rly need to write anyting in program.cs? like i have
using Microsoft.AspNetCore.Mvc;

namespace alpha.Controllers
{
public class LoginController : Controller
{
[Route("login")]
public string Index() => "Login page";
[Route("info")]
public string Info() => "Info page";
}
}
using Microsoft.AspNetCore.Mvc;

namespace alpha.Controllers
{
public class LoginController : Controller
{
[Route("login")]
public string Index() => "Login page";
[Route("info")]
public string Info() => "Info page";
}
}
and thats ok?
wwww
wwww9mo ago
No description
wwww
wwww9mo ago
No description
wwww
wwww9mo ago
but program.cs is like
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();


app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();


app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
and controllers in this case is smthing like wrapper for different routes groupd by similar purpose
Shinigami
Shinigami9mo ago
You can just do app.MapControllers(); instead of mapcontrollerroute Try adding just app.mapcontrollers(); and run the app
wwww
wwww9mo ago
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();

var app = builder.Build();

app.MapControllers();

app.Run();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();

var app = builder.Build();

app.MapControllers();

app.Run();
using Microsoft.AspNetCore.Mvc;

namespace alpha.Controllers
{
public class LoginController : Controller
{
[Route("login")]
public string Index() => "Login page";
[Route("registration")]
public string Info() => "Registration page";
}
}
using Microsoft.AspNetCore.Mvc;

namespace alpha.Controllers
{
public class LoginController : Controller
{
[Route("login")]
public string Index() => "Login page";
[Route("registration")]
public string Info() => "Registration page";
}
}
using Microsoft.AspNetCore.Mvc;

namespace controller.Controllers
{
public class HomeController : Controller
{
[Route("/")]
public string Index() => "Home page";
}
}
using Microsoft.AspNetCore.Mvc;

namespace controller.Controllers
{
public class HomeController : Controller
{
[Route("/")]
public string Index() => "Home page";
}
}
wwww
wwww9mo ago
No description
Shinigami
Shinigami9mo ago
Yes, sir!
wwww
wwww9mo ago
That looks very nice and very comfortable way to deal with routing
Shinigami
Shinigami9mo ago
Isn't it? Builder.service.AddControllers() and app.mapcontrollers() does everything for you I've learnt this recently too, currently learning more. Answering you helped me too, haha.
wwww
wwww9mo ago
Thank a lot for help badhun1UA
Want results from more Discord servers?
Add your server
More Posts