C
C#14mo 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
Shinigami14mo 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
wwwwOP14mo 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
Shinigami14mo ago
Aahh! Apologies I usually use route attributes
wwww
wwwwOP14mo ago
So, here if i go to "/" it will load home/index
Shinigami
Shinigami14mo ago
[Route("/")]
wwww
wwwwOP14mo ago
also i can set up it like
No description
wwww
wwwwOP14mo ago
or like this
No description
Shinigami
Shinigami14mo ago
Yup, right!
wwww
wwwwOP14mo ago
atleast 3 ways and i belive there is more ways to do that
Shinigami
Shinigami14mo ago
I feel route attributes are cleaner, since you wouldn't clutter program.cs
wwww
wwwwOP14mo 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
wwwwOP14mo ago
No description
wwww
wwwwOP14mo ago
No description
wwww
wwwwOP14mo 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
Shinigami14mo ago
You can just do app.MapControllers(); instead of mapcontrollerroute Try adding just app.mapcontrollers(); and run the app
wwww
wwwwOP14mo 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
wwwwOP14mo ago
No description
Shinigami
Shinigami14mo ago
Yes, sir!
wwww
wwwwOP14mo ago
That looks very nice and very comfortable way to deal with routing
Shinigami
Shinigami14mo 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
wwwwOP14mo ago
Thank a lot for help badhun1UA
Want results from more Discord servers?
Add your server