1

This is a very basic question, yet I cannot find any clear, simple, direct answers.

I have a basic MVC4 app with 1 HomeController.cs file. I want to create a second Controller.cs file to put more code into so HomeController doesn't turn into spaghetti code.

So obviously step 1 is to add a new controller. I assume the next step is to add some stuff to RouteConfig.cs.

What do I need to add to RouteConfig.cs to utilize a new Controller.cs?

5
  • Splitting code into two files will not fix a spaghetti code issue... if anything, it'll make it worse. Why not use proper object orientation techniques? Commented Sep 25, 2013 at 14:25
  • 1
    I don't think he actually wants to split HomeController into multiple pieces. I think he just wants to breakup the actions into different logical controllers instead of having unrelated actions all together in one file. Commented Sep 25, 2013 at 14:26
  • There is no need to change RouteConfig.cs if you plan to keep the pattern (controller/action/id). More about routes: asp.net/mvc/tutorials/controllers-and-routing Commented Sep 25, 2013 at 14:27
  • Define more code? Should you be building out another layer? Commented Sep 25, 2013 at 14:28
  • If you want to keep the things in different files physically, you can use Partial Class Commented Sep 25, 2013 at 15:09

4 Answers 4

5

You shouldn't need to add anything. HomeController requires a line of code in your RouteConfig to be set as the default controller (for when users navigate to the site root), but any other controller should be accessible with the default routing.

Just create a controller, add some actions, and you should be able to route to it with the format Controller/Action or using the routing helper functions.

Sign up to request clarification or add additional context in comments.

Comments

3

What does your routes file look like?

Normally, there's a default route:

routes.MapRoute("default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action="Index" }
);

That means that so long as you add a new controller with the Controller suffix, MVC will make sure the routing engine sees your controller, and as long as your URL follows the above structure, requests made in that format will be routed to the appropriate controller.

Comments

0

We normally send it to a different view which submits to different controllers, or add a reference in your current controller if your just wanting to call certain methods in your current home controller.

Comments

0

What you really need first after creating a new controller is to add a new action (if it's not added automatically) and then add a new View for your new action. You need to touch your routes only if you are about to process some specific parameters which dont match your default settings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.