0

I am using Zen barcode framework with MVC, The barcode is rendering just fine but due to adding a new route for to support the barcode in the Route.config

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
         Allow extensionless handling of barcode image URIs
        routes.Add(
            "BarcodeImaging",
            new Route(
               "Barcode/{id}",new BarcodeImageRouteHandler()));

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );


    }

The problem is my action link URLs don't work correctly anymore, they use the barcode route config instead of the default MVC routing

My action Links

 @Html.ActionLink("Edit", "Edit", "RequestTests",new { id=item.RequestTestID },null) |
        @Html.ActionLink("Invoice", "Details", new { id=item.RequestTestID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.RequestTestID })
tereško
  • 58,060
  • 25
  • 98
  • 150
Gerald Kenny
  • 7
  • 1
  • 4

2 Answers2

0

This is happening because you have extended the wrong part of MVC to handle routing. IRouteHandler can only handle incoming URLs.

Instead, you should subclass RouteBase or Route, which are capable of handling both incoming and outgoing URLs. RouteBase has two methods to override

  1. GetRouteData(HttpContextBase) to handle incoming requests (typically converts a URL to a dictionary of route values)
  2. GetVirtualPath(RequestContext, RouteValueDictionary) to handle outgoing requests (typically converts a dictionary of route values to a URL)

In addition, each of these methods must return null when the route does not match the current request to ensure the routing framework will attempt to match all subsequent routes in the route table. This implies that its also the route's job to determine if the route matches the request (which can be done with an if statement).

The UrlHelper class and all extension methods that use it, such as ActionLink all use the GetVirtualPath() method to build URLs, so you must implement it if you want any of these methods to work.

Custom RouteBase Examples

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
0

Change it to

        routes.Add(
            "BarcodeImaging",
            new Route(
                "Barcode/{barcodeId}",
                new BarcodeImageRouteHandler()));

Works fine for me

Björn
  • 1