2

If you load up the Durandal SPA template and try to navigate to a bogus URL (by adding something like "assdf" to the URL), the url is maintained and no error is provided. How can this be changed so that invalid URLs give the user an error page? I think an error is better than leaving the user wondering if their URL worked or not.

RobVious
  • 12,685
  • 25
  • 99
  • 181
  • see edit for handling this sort of thing for "normal" urls as well as hased urls. – mutex Apr 15 '13 at 01:34
  • Thanks so much mutex. Trying your suggestion for clientside now, but I'm running into some other issues: http://stackoverflow.com/questions/16006405/durandaljs-routing-behavior I'll keep trying and post an update/response – RobVious Apr 15 '13 at 01:38

1 Answers1

2

Have you looked at the handleInvalidRoute:

http://durandaljs.com/documentation/Router/

In Hot Towel for example, it is used to show the user an error toast.

EDIT: For example, I created a basic error view, then added this to main.js and it seems to work, though I think I prefer the growl type approach to showing the error to the user in Hot Towel.

    app.start().then(function() {
        //Replace 'viewmodels' in the moduleId with 'views' to locate the view.
        //Look for partial views in a 'views' folder in the root.
        viewLocator.useConvention();

        // ** ADDED **
        router.handleInvalidRoute = function (route, params) {
            router.navigateTo('#/error');
        };

        //configure routing
        router.useConvention();
        router.mapNav('welcome');
        router.mapNav('flickr');

        // ** ADDED **
        router.mapRoute('error', 'viewmodels/error', 'error', false);

        app.adaptToDevice();

        //Show the app by setting the root view model for our application with a transition.
        app.setRoot('viewmodels/shell', 'entrance');
    });

EDIT2: IF you're worried about normal urls too (not hashed ones that will be handled by durandal\sammy) then you'll need to handle it outside of durandal. i.e. you can put something like the following in your global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();

    Response.Clear();

    var httpException = exception as HttpException;

    if(httpException != null) //It's an Http Exception, Let's handle it.
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                // Page not found.
                Response.Redirect("~/#/error");
                break;
        }
    }
}

See here for a more complete approach: How can I properly handle 404 in ASP.NET MVC?

Community
  • 1
  • 1
mutex
  • 7,536
  • 8
  • 45
  • 66