If the method needs to be used in multiple controllers it should not be in AccountController
, at least not logically. I think it's possible to do this by instantiating the controller and calling the method, but this does not seem to be very common, I've never seen anyone doing it, but I can not tell whether it's problematic or not.
There are several ways to make it work the way you want it, I'll give you an example in a simple, easy and quick way.
Create a generic controller named Controller
, so all controllers created in your project will inherit it and you can use this method and others that are created) in any controller .
Note that it is necessary to use the full namespace on the right side of the colon.
public class Controller : System.Web.Mvc.Controller
{
public int CurrentUserId()
{
return Convert.ToInt32(User.Identity.GetUserId());
}
}