When I use the default route:
routes.MapRoute(
"Default", //
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
The app finds the method below:
public ActionResult News(DateTime? FromDate, DateTime? ToDate)
{
IList<Model.MonthGroupNewsEntry> monthGroupNewsEntries = new services.NewsEntryService(new Data.SqlNewsEntryRepository()).GetMonthGroupNewsEntries();
IList<Model.NewsEntry> newsEntries = new Services.NewsEntryService(new Data.SqlNewsEntryRepository()).GetNewsEntriesWithinDateRange(FromDate,
return View(new Models.NewsViewModel(monthGroupNewsEntries, newsEntries));
}
From:
<%= Html.ActionLink(b.MonthName + " " + b.Year.ToString() + " (" + b.EntryCount.ToString() + ")", "News", new { FromDate = b.FromDate, ToDate = b.ToDate}) %>
But when I attempt to remove the controller from the url using below:
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
I get a 404 resource not found error.
Note : The other methods in the class work fine but these either have no parameters or "id", it's only the method that's called with the two DateTime? parameters that fails when I remove the controller from the URL.
Thanks for the updated answer, however - the only change I make is to remove the "{controller}/" text from the "routes.MapRoute" in the above - there is no chance of any spelling mistake etc., this is also using MVC 3.
Further : using Charx's suggestion:
routes.MapRoute(
"SpecificAction",
"{action}/{FromDate}/{ToDate}",
new { controller = "ControllerName", id = UrlParameter.Optional } );
I no longer get a 404 error, but it gave a "400 - bad request" due to the '/'s in the date, so I reformatted the date as dd-MM-YYYY:
<%= Html.ActionLink(b.MonthName + " " + b.Year.ToString() + " (" + b.EntryCount.ToString() + ")", "News", new { FromDate = b.FromDate.ToString("dd-MM-yyyy"), ToDate = b.ToDate.ToString("dd-MM-yyyy")}) %>
It no longer 404's, the FromDate is recognised fine but the ToDate comes through as null.
Final : I changed the type of the two dates to strings and cast them to datetime in the method - now both parameters come through fine - I guess it was perhaps looking for the time element of the first parameter and therefore ignored the second.
Only outstanding thing now is that the methods requiring an "id" parameter now seems to use it as a querystring (NewsEnrty?ID=5) rather than "NewsEntry/5" if anyone can answer this then thanks - otherwise question answered - thanks again Chanx.
Edited. If you want to still be able to identify the action to call in your URL, then you must specify your Specific Action route and be sure to identify the correct Controller the actions are on:
routes.MapRoute(
"SpecificAction",
"{action}/{FromDate}/{ToDate}",
new { controller = "ControllerName", id = UrlParameter.Optional } );
routes.MapRoute(
"Default", //
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );