If the method needs to be used in several controllers he shouldn’t be in AccountController
, at least not logically. I think it is possible to do this by instantiating the controller in question and calling the method, but this does not seem to me to be something very common, I have never seen anyone doing it, but also can not say whether it is problematic or not.
There are several ways to make this work the way you want it, I’ll give you an example in a simple, easy and fast way.
Create a controller generic called Controller
, so all the controllers created in your project will inherit from it and you can use this method (and others that are created) in any controller.
Note that it is necessary to use namespace complete on the right side of the two points.
public class Controller : System.Web.Mvc.Controller
{
public int CurrentUserId()
{
return Convert.ToInt32(User.Identity.GetUserId());
}
}
How so not recognized? What is the error reported by the compiler? You are probably forgetting some reference or
using
...– Genos