ASP.NET MVC and strings prevent me from compiling temporary errors

I am a big fan of compilation errors. I find it much easier to fix than runtime errors. I am currently using the ASP.NET MVC framework, and I find that it has a lot of typo space that the compiler will not catch.

For example, if I want to return a Data view from an Index action.

 public ActionResult Index() { return View("Data"); } 

can be easily mistaken

 public ActionResult Index() { return View("Dats"); } 

and will not throw an error until I run this action.

Has anyone come up with a method for checking these kinds of things at compile time? I came up with two possible solutions:

  • Create a static class with all action names as constants and use names for names. I would have a coding rule that says use only constants. The weakness of this, if I change the name of the view, I have to update the class.
  • Lots of unit tests to make sure that every action of the controller returns a valid ActionResult and runs them anytime I make changes.

What process was implemented to manage this risk? Are there any additions or frameworks that mitigate this problem?

+4
source share
1 answer

The T4MVC project (which recently became part of MvcContrib ) to solve this problem. From the documentation:

T4MVC is a T4 template for ASP.NET MVC applications that create strongly typed helpers that eliminate the use of literal strings when accessing controllers, actions, and views. This helps to greatly enhance your MVC code supported, and gives you intellisense where you usually do not have.

+4
source

Source: https://habr.com/ru/post/1303461/


All Articles