Search code examples
asp.net-mvcroutesasp.net-mvc-routing

ASP.NETMVC routing looping forever


I'm quite new to MVC routing so please bear with me if this is too trivial.

I have created the following route:

routes.MapRoute("ProductSearch", "Category/{CategoryName}/{CategoryID}/{brandName}/{brandID}", new
        {
            controller = "Search",
            action = "Search"
        });
routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );  

This is entering the Action

Search

just fine at least the first time round (with the correct parameter values). Then it will keep looping forever and lose the parameters. Any idea what might be happening?

[EDIT] The issue seems to be coming from the fact that I have 4 placeholders. When I only set 2 placeholders the route worked.

The Action:

public ActionResult Search(string query = null, int CategoryID = 0, int brandID = -1)
    {   
        WebSearch search = null;

        try
        {
            int loyaltyCardID = -1;
            if (FocusStoreRemoting.UserInfo != null)
            {
                loyaltyCardID = FocusStoreRemoting.UserInfo.LoyaltyCardID;
            }

            if (query != null)
            {
                search = FocusStoreRemoting.Controller.DoWebSearch(FocusStoreRemoting.ClientSession.SessionID,
                   FocusStoreRemoting.StoreID, loyaltyCardID, queryString: query);
            }
            else if (CategoryID >= 0)
            {
                search = FocusStoreRemoting.Controller.DoWebSearch(FocusStoreRemoting.ClientSession.SessionID,
                    FocusStoreRemoting.StoreID, loyaltyCardID, groupID: CategoryID, brandID: brandID);
            }
        }
        catch (Exception ex)
        {
            return RedirectToAction("DisplayError", "Error");
        }



Session[SessionStrings.SearchItems] = search.StockItems;

            return View(search.RefineCategories);
}

Thanks in advance.

[Edit2] One thing I have discovered is that it is not looping forever but for each link (and any external file) file I have listed in the <head></head> section of the page


Solution

  • The problem was being caused by links to external files. For example:

    I was calling the external JavaScript files like so:

    src="../../Content/js/whatever"
    

    So when trying to navigate to the file, the absolute URL would be translated to:

    http://localhost/Category/TestCategory/1/TestBrand/1/Content/js/whatever
    

    Which MVC's routing was routing to the Search action mentioned in the Question. So in reality it wasn't "Looping Forever" but for every image/content file the page contained.

    I solved this by removing ../../ changing the relative URL to /Content/js/whatever