0

i have two controllers with the same name in different sub folder My Controllers looks like

  • Controllers
    • api
      • UserController
    • help
      • UserController

I want to access my first controller when the user requests http://mysite/api/User/Index

and access my second controller when the user requests http://mysite/help/User/Index

how to configure routing in Global.asax and how the views folders will look like?

will it look like?

  • Views
    • User
      • api
        • Index
      • help
        • Index

Thanks and Regards.

1 Answer 1

1

You could use namespace constraints:

routes.MapRoute(
    "help",
    "help/{controller}/{action}",
    new { controller = "User", action = "Index" },
    new[] { "MvcApplication1.Controllers.help" }
);

routes.MapRoute(
    "api",
    "api/{controller}/{action}",
    new { controller = "User", action = "Index" },
    new[] { "MvcApplication1.Controllers.api" }
);

As far as having sub-folders for your Views is concerned, this is not supported out of the box. You will have to write a custom view engine for this to work.

By the way have you considered using Areas? They seem like better fit for your scenario. So you would define 2 areas: help and api and have the UserController defined in both.

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

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.