Skip to content

URL rewriting #5

Closed
Closed

Description

@hholtackers

I once built a website that included URL rewriting. For example, /washing-machines/front-loaders/samsung/X5000 was rewritten to /product.aspx?prodId=100. I also implemented this in a sample site that uses the Componyx framework; however, because the view, controller, and server method are determined there via $bindary.addroute, the URL rewriting—which is triggered during startup—doesn't work correctly. I’ve included the function that handles the URL rewriting below.

 public async Task Invoke(HttpContext context)
 {
     var urls = await GetUrls();
     var requestedPath = context.Request.Path.Value;
     var url = urls.FirstOrDefault(x => string.Equals(x.UrlRewrite, requestedPath, StringComparison.OrdinalIgnoreCase));

     if (url != null && !string.IsNullOrWhiteSpace(url.UrlTarget))
     {
         if (url.Url301)
         {
             context.Response.Redirect(url.UrlTarget, true);
             return;
         }
         else
             context.Request.Path = url.UrlTarget;
     }

     await _next.Invoke(context);

 }

Activity

  1. edaanen commented on Nov 9, 2026

    @edaanen
    Contributor

    The rewrite runs on the server, but in a Bindary app the client decides which view to show, based on the URL in the address bar. Since the browser still shows /washing-machines/front-loaders/samsung/X5000, Bindary looks for a route matching that path and finds none. You don’t need a rewrite here; make the pretty URL the route itself and let the server method look up the product.

    Something like this:
    $bindary.addRoute({ path: /^washing-machines\/(.+)$/, viewURL: '/views/product.aspx', controllerURL: '/scripts/product.js', dataRequest: { productPath: '$1' }, dataServerLoad: 'Shop.Product.Controller.Load' });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions