Question Background:
I have a URL that contains a parameter called pageNo
that is sent to a controller called Events
with a method called FutureEvents
that is being called. This is used with pagination to determine the page number of a view that needs to be shown. This is the URL:
http://localhost:xxxxx/Events/FutureEvents?pageNo=1
The Issue:
If the user manually types the URL and doesn't supply the page number parameter in the URL, for example:
http://localhost:51025/Events/FutureEvents
then an ASP error shows with the message:
The parameters dictionary contains a null entry for parameter 'pageNo' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult FutureEvents(Int32)'
This makes sense as the parameter is null. What I want to do is redirect the user to another controller (in this case my Home
Controller and Index
method) when the user tries to call the FutureEvents
controller and Events
method with an empty pageNo
parameter.
This is my current route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The Home
Controller with its Index method - this is what I want to redirect to.
public ActionResult Index()
{
return View();
}
The FutureEvents
Controller with the Events
method:
[HttpGet]
public ActionResult FutureEvents(int pageNo)
{
//code
}