Haradzieniec
Haradzieniec

Reputation: 9340

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?

Upvotes: 6

Views: 8622

Answers (4)

JosephDoggie
JosephDoggie

Reputation: 1594

Remember to do (by hand) any RedirectToAction that references the controller name. By convention, these do not have the word "Controller" in them, which probably does more harm than good (but I digress), and it is not for the faint of heart.

Also, while I appreciate people posting on these forums, things that depend on an external tool that is not free are not exactly great answers. Presumably there is a way to do this without resharper.

Upvotes: 4

Spikeh
Spikeh

Reputation: 3695

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.

Upvotes: 7

Jakub Konecki
Jakub Konecki

Reputation: 46008

You should invest in using T4MVC, so you won't have problems with old 'magic' strings in your views.

Upvotes: 2

bevacqua
bevacqua

Reputation: 48476

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.

Upvotes: 7

Related Questions