0

I have this simple class inside my ASP.NET MVC 3 application:

namespace My.Project.Controllers
{
    public ActionResult Settings()
    {
        var m = GetUserData(userID);
        ...
    }

    private UserModel GetUserData(string id)
    {
        if (string.IsNullOrEmpty(id))
        {
            return null;
        }
        ...
    }
}

Now, I have created a separate area inside the application to house an API. How do I call methods in the first namespace?

namespace My.Project.Areas.api.Controllers
{
    public ActionResult Settings()
    {
        //How to call "GetUserData" from here?
    }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
Glenn Utter
  • 2,313
  • 7
  • 32
  • 44
  • 1
    Short answer: you don't call action methods in other controllers. Longer answer: Split out the shared logic into it's own class. – DavidG Aug 02 '16 at 09:26

1 Answers1

0

Do not do this. The private method should be in your business logic layer.


But if you wanna do it necessarily, make these changes

In the first controller, make the function signature public

public UserModel GetUserData(string id)

In the second controller, add this namespace.

using My.Project.Controllers;

Now inside the method write this.

public ActionResult Settings(string id)
{
    var controller = new FirstController(); //argh
    var result = controller.GetUserData(id);
}
naveen
  • 53,448
  • 46
  • 161
  • 251