Below are the ways, the views are not called by the controller actionMethod.
Method 1: Having the common folder called shared. Views in shared folder donot require a controller actionMethod. When ever an action method executes and returns a view that is not located in its path. Then it will check the shared folder by that view name.
Ex: return view("NoData")
Here the NoData is searched in shared folder if is not located in the views folder corresponding to that controller.
Method 2: If there are 100 views with some static data, then does it make sense in creating 100 actionMethods in controller. Obviously no, in such a case, the MVC framework helps us with the method HandleUnknownAction that executes whenever an attempt to invoke the action method that doesnot exist.
protected override void HandleUnknownAction(string actionName)
{
try
{
this.View(actionName).ExecuteResult(this.ControllerContext);
}
catch(Exception ex)
{
Response.Redirect("PageNotFound");
}
}
Code Snippet:
public class HomeController : Controller
{
protected override void HandleUnknownAction(string actionName)
{
try
{ this.View(actionName).ExecuteResult(this.ControllerContext);
}
catch(Exception ex)
{
Response.Redirect("PageNotFound");
}
}
public ActionResult Index()
{
return View();
}
}
No comments:
Post a Comment