how to create 2 controllers with same actionresult 2 views but one implementation

Posted by amalatsliit on Stack Overflow See other posts from Stack Overflow or by amalatsliit
Published on 2013-06-25T23:02:33Z Indexed on 2013/06/26 22:21 UTC
Read the original article Hit count: 120

Filed under:
|

I got a controller in my mvc application like below.

public class BaseController: Controller
{
    protected void LogInfo()
    { 
        logger.InfoFormat("[SessionID: {0}, RemoteIP: {1}]", Session.SessionID,    Request.UserHostAddress); }
}

public class FirstController : BaseController
{

     public ActionResult Index(string name)
     {
        LogInfo();
        getQueryString();

        if(IsValidRec())
        {
            if(Errors()))
            {
               return View("Error");
            }

            var viewname = getViewName(name);
            return view(viewname);
        }
        else
            return view("NotFound"); 

     }
}

I need to create another controller(SecondController ) with same ActionResult method that FirstController has but without any implementation. Because I dont wont to repeat same code in 2 ActionResult methods.

what is the best way to do that. I tried in following way but I m getting error when I initialize my protected method 'LogInfo()'

public class SecondController : BaseController
{

     public ActionResult Index(string name)
     {
        var firstcontroller = new FirstController();
        return firstcontroller.Index(name); 
     }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc