0

I have a next route:

routes.MapRoute(
    "CatalogFilter",
    "{url}Catalog.aspx/{fltr}",
    new { controller = "Catalog", action = "Index", page = 1 }
);

So link does not match that route: http://localhost:63515/MotorOilCatalog.aspx?fltr=156 instead of http://localhost:63515/MotorOilCatalog.aspx/156.

I tried to remove all other routes to be sure that there are no unambiguous or conflicted routes but it also does not work.

I installed Phil Haacks "Route Debugger" and it shows: list of routes

mykhailovskyi
  • 680
  • 1
  • 8
  • 19

1 Answers1

0

To build a link that matches this route:

routes.MapRoute(
    "CatalogFilter",
    "{url}Catalog.aspx/{fltr}",
    new { controller = "Catalog", action = "Index", page = 1 }
);

You need to specify all of the route values that exist in the route. You have 5 values:

  1. controller
  2. action
  3. page
  4. url
  5. fltr

So you need to supply all 5 values to match the route from an ActionLink. If you want to generate the URL /MotorOilCatalog.aspx/156, you have to make the ActionLink like this:

@Html.ActionLink("my link", "Index", "Catalog", new { page = 1, fltr = 156, url = "MotorOil" }, null)

Do note that the way you have it configured, the only way to override a page number from the URL is to add it to the query string.

/MotorOilCatalog.aspx/156?page=2

Since your question is unclear, I am assuming of course that this is an MVC application, and that you have a Catalog controller in your application with an Index method.

public class CatalogController : Controller
    public ActionResult Index(string url, int fltr, int page)
    {
        // Implementation

        return View();
    }
}

If this is in fact an ASP.NET application, you should be using MapPageRoute instead of MapRoute to build your routes to map them to physical pages instead of controllers.

Reference: https://msdn.microsoft.com/en-us/library/cc668177.aspx

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks for reply. It is an MVC application (.aspx in the routes just for supporting old links). There are 4 routes for Catalog/Index: each for different number of input parameters. ActionLink in view looks like you suggested, but it does not work. – mykhailovskyi Sep 21 '15 at 14:51
  • Please post all of the routes in your configuration. Most likely another route is matching before this one (that is, it matches when all of these route values are passed). The first route registered in the configuration that matches always wins. – NightOwl888 Sep 22 '15 at 16:54
  • It's true that first matched route wins, but I tried commented all routes except of one I posted in question and default one. And even though my custom route is top, default route is matched instead of my expectation. – mykhailovskyi Sep 23 '15 at 09:14
  • Have a look at [this question](http://stackoverflow.com/questions/2310187/asp-net-mvc-route-ids-with-a-period). The period is not allowed in an MVC route with the default settings. You need to make some adjustments to the configuration file. Assuming IIS 7+ and .NET 4+, use ``. – NightOwl888 Sep 23 '15 at 10:37