locked
HTML Client custom routing question RRS feed

  • Question

  • Hi all,

    I want to build some custom routes from the MVC portion of an application to the LightSwitch portion in order to make friendly urls like: HTMLClient/Employees.  This is easy enough, but say I have a typical HTML client url: HTMLClient/#/BrowseEmployees/[c9c568f66], how do you account for the [c9c568f66] portion?  I have noticed you can set it to anything and it will work, (e.g. [0] works), but that just doesn't feel right.  Any way to set that portion of the url the "right way"?  Thanks!

    Monday, December 7, 2015 5:25 PM

All replies

  • We use a generic pattern like the following for such links where we specify both the screen name and one or more parameter values using p0, p1, p2, etc:

    http://locahost/HTMLClient/?screen=MyScreenName&p0=123&p1=456&p2=null

    We amend the .ready() function in default.html to look like this:

    $(document).ready(function () {
        // can optionally specify startup screen as URL param (screen will read screen params from URL)
        var screen = getUrlParameter("screen");
        msls._run(screen)
        .then(null, function failure(error) {
            alert(error);
        });
    });


    In the created function of those screens that can be directly linked to from the outside we extract the values of p0, p1, p2, etc as required to load data like follows:

    var clientId = getUrlParameter("p0");
    if (clientId != null) {
        screen.ClientId = clientId;
    };
    
    var orderId = getUrlParameter("p1");
    if (orderId != null) {
        screen.OrderId = orderId;
    };
    
    ...etc...

    The helper functions look as follows:

    // URL parameter (returns null if not found)
    function getUrlParameter (name) {
        var match = (RegExp(name + '=' + '(.+?)(&|$)').exec(window.location.search) || [, null])[1];
        if (match === "null") return null;
        if (match) {
            return decodeURI(match);
        } else {
          return null;
        };
    };
    
    // And the integer version
    function getUrlParameterInt(name) {
        var value = getUrlParameter(name);
        if (stringNullOrEmpty(value))
            return null;
        return parseInt(value, 10);
    };
    
    // Is the string null or empty?
    function stringNullOrEmpty(s) {
        return (!s || s.length == 0);
    };

    Hope this helps.


    Regards, Xander. My Blog


    • Edited by novascape Tuesday, December 8, 2015 12:37 AM
    • Proposed as answer by Angie Xu Tuesday, December 15, 2015 5:54 AM
    Tuesday, December 8, 2015 12:37 AM
  • Thanks Michael and Xander!  I am definitely going to have a use for the custom routing with parameters.  I'm still a little confused about what msls is doing by appending the [xxxxxxxxx] to the end of each url.  I suspect that is just to keep the brower history in order so that the back button works, but I don't know how to replicate it in custom routes, or if I even need to worry about it.
    Tuesday, December 8, 2015 3:58 AM