I have an MVC app that has 2 controllers.
Home and Details.
Details Controller:
public ActionResult Index(long startTime, long endTime, string kpiName)
{
// create my viewmodel.
return View(detailsViewModel);
}
This page should only be called if the user clicks a details button on another page. The problem is the user can directly navigate to http://localhost:54816/Details and a YSOD is shown with the error
The parameters dictionary contains a null entry for parameter 'startTime' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult Index(Int64, Int64, System.String)' in 'SentlianKPI.Controllers.DetailsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
I tried adding a route as shown below:
routes.MapRoute(
name: "Details",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Details", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
With the idea being that the id is not optional so it will fail through to the Home controller.
It's not working though, how do I setup a route to catch this and redirect the user?