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?
    }
}
1
  • 1
    Short answer: you don't call action methods in other controllers. Longer answer: Split out the shared logic into it's own class. Commented Aug 2, 2016 at 9:26

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.