View Without Controller Action in ASP.NET MVC
In MVC, the request from browser actually comes in the form of "controllername/actionname" where the request is actually directed to actionname in the controller which then capable of returning a response in the form of views or files or may be redirected to action of another controller. But sometimes there is requirement when views need not to be associated with an action. An error is thrown when browser requested action is not found in the controller. In such scenario "Controller.HandleUnknownAction" find it's usage. This method is called whenever requested action by browser is not found in controller and in this action a view can be returned or redirection to another controller action etc. Therefore in this way view need not to be associated with a particular action.
There is a scenario when various actions are only returning a view, in such cases there is a possibily of creation of thousand of actions in a controller,this problem can be fixed by using "Controller.HandleUnknownAction" method.
Below is an example of HandleUnknownAction method :-
Create a Controller "Home" in Controllers folder in ASP.NET MVC application with an index action :-
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
Create a view named "Get_View_Info" in Home folder in Views folder but this view does not have action associated with it.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Get_View_Info</title>
</head>
<body>
<div>
<p>This view is without an action method!!!</p>
</div>
</body>
</html>
Whenever browser makes a request of "Home/Get_View_Info" following error occurs :
![](http://findnerd.s3.amazonaws.com/imagedata/7245/7245.png)
The above error occur since requested action "Get_View_Info" is not found in "Home" controller. There exists "Get_View_Info" view. For execution of "Get_View_Info" view "Home" controller is changed by adding and overriding HandleUnknownAction method of "Controller" class.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
protected override void HandleUnknownAction(string actionName)
{
this.View(actionName).ExecuteResult(this.ControllerContext);
}
}
When again browser makes a request of "Home/Get_View_Info" then since no associated action is found request is redirected to HandleUnknownAction method which further calls the "Get_View_Info" view. This method will get called in cases when no associated action are found and thus displaying corresponding view.
![](http://findnerd.com/imagedata/7248/7248.png)
But what if browser makes a request which does not have associated action as well as no view is associated to it then following error occur :-
![](http://findnerd.s3.amazonaws.com/imagedata/7249/7249.png)
To fix such problem use try catch block and redirect to error page if no corresponding action or view is found.
public class HomeController : Controller
{
protected override void HandleUnknownAction(string actionName)
{
try
{
this.View(actionName).ExecuteResult(this.ControllerContext);
}
catch(Exception ex)
{
Response.Redirect("error");
}
}
}
Following screen appear when browser request correspond to no action and view i.e it is redirected to error page.
![](http://findnerd.s3.amazonaws.com/imagedata/7247/7247.png)
0 Comment(s)