Dwayne Love
Dwayne Love

Reputation: 343

Aurelia not loading URLs without hash

I have read the other questions on this issue and the answers didn't seem to help me. Maybe because I'm using ASP CORE. If I navigate to http://localhost:5000/#home the routing works fine. But when I remove the hashtag, the page does not load. Here is my routing code:

import {Redirect, NavigationInstruction, RouterConfiguration} from 'aurelia-router';

export class App {
  configureRouter(config: RouterConfiguration): void {
    config.title = 'xxxx';
    config.options.hashChange = false;
    config.options.root = '/';
    config.map([
      { route: ['home'], name: 'home', moduleId: 'views/home' },
      { route: '', redirect: 'home'}
    ]);
  }
}

I've also tried adding this:

config.options.pushState = true;
config.options.hashChange = true;

Upvotes: 3

Views: 528

Answers (2)

dpix
dpix

Reputation: 3103

Instead of a redirect you can just set multiple routes to the same moduleid e.g.

config.map([
      { route: ['home'], name: 'home', moduleId: 'views/home' },
      { route: '', name: 'home2', moduleId: 'views/home'}
    ]);

if you are happy with having # in your urls don't worry about changing the push state settings

Upvotes: 0

Simon Farrugia
Simon Farrugia

Reputation: 100

If you are using asp.net core you need to setup server side routing to redirect your requests to index.html. In your Startup.cs in the Configure method you need to do something like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.Use(async (context, next) =>
        {
            await next();

            if (context.Response.StatusCode == 404
                && !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/index.html";
                await next();
            }
        });

        app.UseStaticFiles();
    }

Upvotes: 1

Related Questions