I’ve finally decided to dive into .NET Core. Trepidatious at first, it took about an hour until I was hooked. Having just dove into Node.js and Express, ASP.NET Core felt a little like an old friend. However, this is not what this post is about. It’s about the lambda operator and how to use them to shorten your controllers. I knew I had seen a few controller actions written like this in a github project somewhere but I couldn’t find any information on it anywhere, so this might save you some trouble if you’re looking for this like I was.
The Lambda Operator (=>)
From Microsoft:
The=>
token is called the lambda operator. It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side. Lambda expressions are inline expressions similar to anonymous methods but more flexible; they are used extensively in LINQ queries that are expressed in method syntax. For more information, see Lambda Expressions.
If you’ve done anything with LINQ, you’re probably already familiar with the lambda operator:
db.Collection.OrderBy(m=>m.Name)
But you can do some other cool things with it too!
One-liner MVC Controller Actions
Let’s look at the default scaffolded HomeController in a brand-new ASP.NET Core MVC project. For the sake of this example, I’m going to omit the ViewData[“Message”] lines
public class HomeController : Controller { public IActionResult Index() { return View(); } public IActionResult About() { // ViewData["Message"] = "Your application description page."; return View(); } public IActionResult Contact() { // ViewData["Message"] = "Your contact page."; return View(); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } }
You will notice that there are roughly 35 lines of code to display 5 different web pages. It’s always irked me that displaying just the Index view requires 4 lines of code.
Lambda operators can help fix that with something called expression body definitions. Long story short, you don’t have to use the return keyword or braces to write your function. What that means is that we can turn a lot of controller actions into one-line wonders. This will not work in a case where you need to do more than just return a view, but it’s perfect for those pesky actions that don’t do anything else than return a view. (That’s why I took the ViewData[“Message”] lines out) This is the modified code after using lambdas and expression body definitions:
public class HomeController : Controller { public IActionResult Index() => View(); public IActionResult About() => View(); public IActionResult Contact() => View(); public IActionResult Privacy() => View(); [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() => View(new ErrorViewModel{RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier}); }
That’s just a 5th of the original code. You can use PartialView the same way and even pass in models.