9 ms·
Things start getting a little verbose with everything on 1 line thrown inside a function. app.MapGet("/todos/{id:int}", [Authorize("AdminsOnly")] (int id) =>
by DonnyV 5y ago
Things start getting a little verbose with everything on 1 line thrown inside a function.
app.MapGet("/todos/{id:int}", [Authorize("AdminsOnly")] (int id) => "This endpoint is for admins only");
Attributes make things cleaner with everything having its own line. Also you lose the ability to group a bunch of methods in 1 controller together under the same authentication policy.
- moogly 5y agoThere is a multitude of ways in which you could format that code: app.MapGet( "/todos/{id:int}", [Authorize("AdminsOnly")](int id) => "This endpoint is for admins only"); Or for people who don't like expression bodied members: app.MapGet( "/todos/{id:int}", [Authorize("AdminsOnly")](int id) { return "This endpoint is for admins only"); }); Extracted to a method: app.MapGet("/todos/{id:int}", GetTodos); [Authorize("AdminsOnly"] string GetTodos(int id) => "This endpoint is for admins only"; And more... You could also put these methods in a class and put the attribute on the class, which would solve your "grouping" problem.