Appcache in Asp.net MVC

Asked

Viewed 111 times

2

I need to use the Html5 appcache to store some web pages. Before using MVC I simply did this regular expression to check if you are connected or not.

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

And in this loop I take the 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 , only now I will work with MVC , I will 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, gave error in the regular expression. Only it didn’t work, someone has some idea of how to do ?

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 bars, you need to 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$/;

In the case of a single Controller, the expression can be simplified to:

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

Keep in mind, however, that using Asp.net mvc, the routes are not directly mapped to the files of your solution, and therefore it makes no sense to associate them to the extensions of these files. In your case, simply remove the suffix from the expression .htm. The final expression, in this case, would be:

var offlinePages = /^\/Controller\/(Index|About)$/;
  • Thanks for the help, I got another question , how to save these views in the manifest file , I did : /home/index /home/about , only it didn’t work , I also tried to put views/home/index.cshtml only that it also wasn’t..

  • /Home/Index and /Home/About should work. I suggest you create a new question for this problem by giving more information (for example, how is the rest of your manifest file? how you referenced it in html?).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.