locked
How do I create an Interface method that gets the Controller.....? RRS feed

  • Question

  • User-507786106 posted

    How do I create an Interface method that gets the Controller Name and the Action name?

    So far all my Controller Actions have the following Code, and I would like to simplify this code into one location and call it into each action.

    var controllName = ControllerContext.RouteData.Values["action"].ToString();
    var actionName = ControllerContext.RouteData.Values["action"].ToString();

    I have a seperate project called Burns.Services in the project is where I keep my services which is seperate from my controllers.
    However, I need one service to do the work of getting the controllname and actioname.

    Can you provide your expertise the help me,
    thank you

    Wednesday, May 8, 2019 3:58 PM

Answers

  • User1520731567 posted

    Hi slimbunny,

    I can't quite understand what your mean.

    Do you mean you want to create a common Interface method which return Controller Name and the Action name,

    then you call this Interface to get current Controller Name and the Action name?

    If so,why not use the below code in your :

    string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
    string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();

    or you could make a common method in BaseController and then call CurrentAction  and CurrentController in your current action(I perfer to the first one):

    public class BaseController : Controller
    {
        protected string CurrentAction { get; private set; }
        protected string CurrentController { get; private set; }
    
        protected override void Initialize(RequestContext requestContext)
        {
            this.PopulateControllerActionInfo(requestContext);
        }
    
        private void PopulateControllerActionInfo(RequestContext requestContext)
        {
            RouteData routedata = requestContext.RouteData;
    
            object routes;
    
            if (routedata.Values.TryGetValue("MS_DirectRouteMatches", out routes))
            {
                routedata = (routes as List<RouteData>)?.FirstOrDefault();
            }
    
            if (routedata == null)
                return;
    
            Func<string, string> getValue = (s) =>
            {
                object o;
                return routedata.Values.TryGetValue(s, out o) ? o.ToString() : String.Empty;
            };
    
            this.CurrentAction = getValue("action");
            this.CurrentController = getValue("controller");
        }
    }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2019 9:53 AM

All replies

  • User1520731567 posted

    Hi slimbunny,

    I can't quite understand what your mean.

    Do you mean you want to create a common Interface method which return Controller Name and the Action name,

    then you call this Interface to get current Controller Name and the Action name?

    If so,why not use the below code in your :

    string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
    string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();

    or you could make a common method in BaseController and then call CurrentAction  and CurrentController in your current action(I perfer to the first one):

    public class BaseController : Controller
    {
        protected string CurrentAction { get; private set; }
        protected string CurrentController { get; private set; }
    
        protected override void Initialize(RequestContext requestContext)
        {
            this.PopulateControllerActionInfo(requestContext);
        }
    
        private void PopulateControllerActionInfo(RequestContext requestContext)
        {
            RouteData routedata = requestContext.RouteData;
    
            object routes;
    
            if (routedata.Values.TryGetValue("MS_DirectRouteMatches", out routes))
            {
                routedata = (routes as List<RouteData>)?.FirstOrDefault();
            }
    
            if (routedata == null)
                return;
    
            Func<string, string> getValue = (s) =>
            {
                object o;
                return routedata.Values.TryGetValue(s, out o) ? o.ToString() : String.Empty;
            };
    
            this.CurrentAction = getValue("action");
            this.CurrentController = getValue("controller");
        }
    }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2019 9:53 AM
  • User-507786106 posted

    Yes, I need a common interface that captures and save / return the controller name and action name.

    I am tracking the history of the user actions and one is the now the controller name and action....

    Monday, May 13, 2019 3:10 PM