Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rename Controller

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?

like image 981
Haradzieniec Avatar asked Jan 21 '13 13:01

Haradzieniec


People also ask

How do I rename a controller in Visual Studio?

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.

Can you change the action method name?

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.


1 Answers

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.

like image 135
bevacqua Avatar answered Nov 15 '22 09:11

bevacqua