Avoid hard-coding controller and action names

前端 未结 5 814
攒了一身酷
攒了一身酷 2020-12-24 15:19

ASP.NET MVC seems to be encouraging me to use hard-coded strings to refer to controllers and actions.

For example, in a controller:

return RedirectT         


        
5条回答
  •  盖世英雄少女心
    2020-12-24 16:08

    Look at T4MVC this generates classes so you can have strongly typed actions and controller names. As it's still just a mapping to a string, refactoring won't cause the names in your views to update if you change a controller name for example.

    After regenerating you will get compilation errors due to the names disappearing from your generated classes though so it's still useful in refactoring and catches problems that you can miss using hard-coded strings.

提交回复
热议问题