SPA on the MVC default route

4 posts, 0 answers
  1. Will
    Will avatar
    40 posts
    Member since:
    Oct 2013

    Posted 17 Sep 2018 Link to this post

    Hi,

    I'm doing my first SPA application and things are going relatively well. 

    One annoying glitch that I've noticed is that since my app is on the default route, the router reacts to the fully qualified route as a different page and reloads it.

    E.g. http://localhost:5849/#/alert is not the same as http://localhost:5849/Home/Index/#/alert even though it really is.

    I can certainly understand why this is happening.

    My question, before I go off and spend a couple days coming up with my own unique hack for this, is there an established method for handling this situation?

    Thanks,

    Will

  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    2025 posts

    Posted 19 Sep 2018 Link to this post

    Hi Will,

    The router tracks navigation through the fragment part of the URL. Using the default route:
    router.route("/", function() {

    in the scenario you described that would mean tracking changes to the highlighted part only:

    http://localhost:5849/#/alert

    and

    http://localhost:5849/Home/Index/#/alert

    whereas changes to the controller and action in the URL are handled by MVC.

    As for preventing navigation with the Router in general, this can be done by preventing its change event conditionally, e.g.:
    if (e.url == "/test") {
        e.preventDefault();
    }


    Regards,
    Ivan Danchev
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Will
    Will avatar
    40 posts
    Member since:
    Oct 2013

    Posted 19 Sep 2018 in reply to Ivan Danchev Link to this post

    Ivan,

    Thanks for the clarification. 

    In my limited understanding of the Router I thought that it was intercepting the changes to the Url before they were sent to the server and that there might be an opportunity to manipulate them.

    Maybe the answer then is just not to have an SPA on the default MVC route, forcing the Url to always be the complete path.

    Thanks,

    Will

  4. Ivan Danchev
    Admin
    Ivan Danchev avatar
    2025 posts

    Posted 21 Sep 2018 Link to this post

    Hi Will,

    Yes, with the default MVC route the controller and action can be accessed even if not explicitly specified, e.g., http://localhost:5849, so using a route that requires the controller and action to be specified is a valid approach if you don't want the page to be accessible (and reloaded) through two different Urls.

    Regards,
    Ivan Danchev
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top