Appcache in Asp.net MVC

2

I need to use the html5 appcache to store some web pages. Before using MVC I simply made this regular expression to check whether it is connected or not.

var offlinePages = /^\/(index|about|schedule|location).htm$/;

And in this loop I test:

var hideLinksThatRequireOnline = function () {
    var allNavLinks = document.querySelectorAll("nav.page-nav a");
    for (var i = 0; i < allNavLinks.length; i++) {
        var href = allNavLinks[i].getAttribute("href");
        if (!offlinePages.test(href)) {
            allNavLinks[i].style.display = "none";
        }
    }
};

So far so good, but now I'm going to work with MVC, I'm going to have Controlers and Actions, so I had to change the expression:

var offlinePages = /^\/(Index|About).htm$/;

I did not put Controller / Index, because when I put the bar, it gave error in the regular expression. It just did not work, does anyone have any idea how to do it?

    
asked by anonymous 15.08.2014 / 16:17

1 answer

3

In javascript, the / bar is reserved to indicate the beginning or end of the regular expression. In order for your expression to accept strings that contain slashes, you must use an escape character - which in this case would be the backslash \ . In this case, the resulting expression would be:

var offlinePages = /^\/(Controller\/Index|Controller\/About).htm$/;

If it is a single Controller, the expression can be simplified to:

var offlinePages = /^\/Controller\/(Index|About).htm$/;

Keep in mind, however, that using asp.net mvc, routes are not mapped directly to the files in your solution, so it does not make sense to associate them with the extensions of these files. In your case, just remove the suffix .htm from the expression. The final expression, in this case, would be:

var offlinePages = /^\/Controller\/(Index|About)$/;
    
15.08.2014 / 17:46