Tuesday, September 27, 2016

Using ASP.NET MVC Futures to eliminate Magic Strings

If you have used RedirectToAction throughout your ASP.NET MVC Controllers, you have probably experienced the frustration of having to manually type in string names of controllers as follows:


However, with ASP.NET MVC Futures, you can eliminate typing in these "Magic Strings" for these Redirects and instead rely on strong typing and using lambda expressions!

In order to get started, you first have to install Microsoft.AspNet.Mvc.Futures as a NuGet package:




Once you have installed this NuGet package, you will end up with a reference to "Microsoft.Web.Mvc".  You will have to add a using statement for this library to the top of your MVC Controller file.

Once you have done that, you can easily replace this RedirectToAction functionality in your Controller methods as follows:



That is all there is to cleaning up numerous "Magic Strings" from your ASP.NET MVC Web Application!

No comments:

Post a Comment