I have two routes
routes.MapRoute(
name: "Default",
url: "{location}/{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
When user goes to main page, he has to chose two links:
<a href="/loc1/Products/index">Location 1</a>
<a href="/loc2/Products/index">Location 2</a>
Also I have ActionFilterAttribute
, where I check for location
parameter, and if url does not have it, i redirect users to main home page.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string location = (string)filterContext.RouteData.Values["location"];
if (string.IsNullOrEmpty(location)
&& filterContext.ActionDescriptor.ControllerDescriptor.ControllerName != "Home"
&& !filterContext.IsChildAction)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "controller", "Home" },
{ "action", "Index" }
});
}
SessionUtils.SetLocationName(location);
}
Based on the location
variable I will query different records from database, so when user does not provide any location in url, and tries to access Products/index/5
I want them to be redirected to the main page where they have to choose location and click one of the links.
However I get error message:
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Products/index
How should I properly configure routes, so that users would be redirected to main page if there is no location parameter in url?