0

I am working on asp.net mvc app in which i have to perform CRON job to execute this:

==> I have to execute webpage of my application (which contains ajax request) after every two mins. It like tracking someone time to time.

(it is basically executing google map api so usage of javascript is compulsory)

I am wondering how to execute webpage containing javascript same as we execute on browser. I m invoking Application_BeginRequest function in MVC that is called by cron job after every two mins. So how to execute demo page containing javascript.

I have searched alot but did'nt find any useful resources.

Edit: Working of application:

1- CRON job calls Application_Begin function after every two mins

2- Then i want to call DemoPage which contains javascript and ajax request. first when Demo page requested, ajax call made to server which returns some data 3. Then from ajax success, i execute response data with some other data using javascript google map api ( with RouteBox, locating latlng on route ).

3- If point is not in route, it will send alert email.

Its little difficult to explain what i want, but i guess you got my point ?

MORE DETAILS:

When page requested, it returns json string of route, and i have list of latlngs which i need to test against that json string which is done by RouteBox class written in javascritp.

Hamza Zaidi
  • 401
  • 7
  • 15
  • your question is confusing. It sounds like you want to track a user that has your page open. All you need is javascript `setInterval` to call the api every 2 minutes. I can't imagine invoking BeginRequest is ever a good idea or how it would correlate that fake request with a user. If you are not tracking an actual user on your site you can use `HttpClient` to call their REST api without javascript. Or you can run a javascript engine like V8 on the server. But those would track the server which I imagine stays pretty still. – Crowcoder Mar 18 '16 at 10:51
  • Whose location are you trying to get? – Crowcoder Mar 18 '16 at 11:44
  • the latlng are stored in database, like latlngs of 10 cars. I can do everything if i know how to call webpage automatically – Hamza Zaidi Mar 18 '16 at 11:49
  • Even if you figure out how to invoke a web page on the server you will always get the server location. To get a client location, the code has to execute on the client, and it sounds like you don't have one. – Crowcoder Mar 18 '16 at 11:54
  • i dont want to get location of anyone, i have locations in my database i just want server to execute javascript on that page – Hamza Zaidi Mar 18 '16 at 12:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106732/discussion-between-crowcoder-and-hamza-zaidi). – Crowcoder Mar 18 '16 at 14:40

1 Answers1

1

You can return script from your controller:

public ActionResult SampleAction()
        {
            return JavaScript("alert(Sample!');");
        }
S. Nadezhnyy
  • 582
  • 2
  • 6
  • And how to call sampleAction from Application_BeginRequest in globle.asax – Hamza Zaidi Mar 18 '16 at 10:49
  • you can add script like this : context.Response.ContentType = "application/x-javascript"; context.Response.Write("alert(alert(Sample!')"); But I suppose Application_BeginRequest is not designed for that purpose. – S. Nadezhnyy Mar 18 '16 at 10:58
  • Basically, if when page executed by user, the whole javascript code is executed and send alert mails to admin. I want to perform this action after every two mins automatically but not by user so thats y i am croning this task – Hamza Zaidi Mar 18 '16 at 11:09
  • Than in your case it is better do this on server side without any javascript. Use some SMTP client and make a Thread or Task for sending emails. – S. Nadezhnyy Mar 18 '16 at 11:14
  • But how to test latlngs without google map api (javascript). the mails will be send on the basis of results of latlng – Hamza Zaidi Mar 18 '16 at 11:19
  • to get geolocation you don't realy need google map. You can get them with navigator.geolocation.getCurrentPosition(function (position) { //position.coords.latitude; position.coords.longitude; }); and send them to server method. – S. Nadezhnyy Mar 18 '16 at 11:26
  • my basic question is, how to execute web page (with all scripts and ajax requests) automatically. (same as user request a page). I dont want to get any output just execute the page – Hamza Zaidi Mar 18 '16 at 11:43
  • What do you mean by "execute page" ? Page is some HTML + CSS + scripts. Page by itself is not executable. But you can preform script execution's on page f.e. in document.ready() - it is good function to place server callbacks with latitude/longitude. – S. Nadezhnyy Mar 18 '16 at 11:50
  • a big javascript is written in the page and i want to call the page inorder to execute the script. How to do this ? – Hamza Zaidi Mar 18 '16 at 11:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106710/discussion-between-hamza-zaidi-and-s-nadezhnyy). – Hamza Zaidi Mar 18 '16 at 11:57