I'm studying Getting Started with ASP.NET MVC3 chapter from asp.net. I've just created the MoviesController
controller but I made a typo. How to rename it (so it doesn't break anything). Unfortunately, I didn't find in the Solution Explorer how to rename (refactor) the controller so all name changes apply through the whole project. As least, the controller .cs
file , Views folder and probably some names in the files should be renamed. What is the proper way to do that?
With Visual Studio: Highlight the controller name in your editor, and press F2. You'll have to rename folders yourself. Alternatively, install Resharper and use the refactoring in it... it makes renaming directories and files very easy, but requires a little bit of time to learn it.
To rename the Action method of the controller, we use ActionName attribute to the action method of the controller. Here, despite that our Action method name is Index, it will be accessible from the browser as PersonList. Eg.
I feel ReSharper is a tool that can't be missing in your Visual Studio configuration. It's a productivity add-on that allows you to very easily refactor your code, clean it up, find issues, and find files or classes.
For example, for your use case, you simply place your caret on the controller name and use the refactor shortcut hotkey to rename it, and it will rename all usages of the class, and in the case of controllers it will do so in views as well.
Very handy. to me it's a requirement. I've been using it for three years now, never looked back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With