Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rename controllers asp.net mvc

I have a bigger project with about 9 controllers. Now at the end of the project the url requeirement change. How to deal best with this situation - renaming the controllers seems just too cumbersome ... I need to change all links in servercode and javascript

like image 327
Marc Loeb Avatar asked Apr 24 '12 14:04

Marc Loeb


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. Save this answer.

Can we change action name in MVC?

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.

What is controller in asp net core MVC?

The controller takes the result of the model's processing (if any) and returns either the proper view and its associated view data or the result of the API call. Learn more at Overview of ASP.NET Core MVC and Get started with ASP.NET Core MVC and Visual Studio. The controller is a UI-level abstraction.


1 Answers

Your problem can be solved by changing your existing routes. In your global.asax you'll find a code fragment like this

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

That maps an URL '/Controller/Action/Id' to Controller, Action and Id. You can provide routes like this

    routes.MapRoute(
            "RefactoredRoute", // Route name
            "SomeChangedURLBase/{action}/{id}", // URL with parameters
            new { controller = "Controller", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

to route requests to /SomeChangedURLBase... to be handled by Controller.

Be aware that these routes should be registered before the default route to avoid that links generated in views point to the default route and generate the old URL.

like image 73
saintedlama Avatar answered Sep 28 '22 08:09

saintedlama