Wednesday, July 31, 2013

MVC -HandleUnknownAction -Avoid Redundant Code Action Method in Controller

HandleUnknownAction:

Just overirde Controller Method HandleUnknownAction- Now we can merge redundant method which does the same thing of loading view .Such view does nothing just rendering view. There can be scenario when the action is called but there is no relevant action defined in controller in such case we can override this method to handle unknown calls.

public class CustomerController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Details()
    {
        return View();
    }
   
    public ActionResult Help()
    {
        return View();
    }
}

Just overirde Controller Method HandleUnknownAction- Now we can merge redundant method which does the same thing of loading view .Such view does nothing just rendering view. There can be scenario when the action is called but there is no relevant action defined in controller in such case we can override this method to handle unknown calls.

public class CustomerController : Controller
{
    protected override void HandleUnknownAction(string actionName)
    {
        this.View(actionName).ExecuteResult(this.ControllerContext);
    }
}
Post a Comment