0

When I start my MVC application I want to call:

$.ajax({
   url: "@Url.Action("CreateInstance", "Home")",
   type: "POST"
});

This works! But I want to call it only once. If I switch from page to page this function is still getting called. I only want to call the function if I start the project. Is this possible?

Jeffrey
  • 183
  • 1
  • 2
  • 14
  • 1
    On the first request of the application / application start? Or the first request of a user? – Steven V Nov 26 '13 at 14:46
  • First request of the application. If I press start in Visual Studio – Jeffrey Nov 26 '13 at 14:47
  • You should probably describe the actual problem you're trying to solve here, because there is probably a better solution then making a ajax call from the client when the application starts. – Mattias Jakobsson Nov 26 '13 at 14:55
  • I have a constructor that will fill a list. But it only goes to the constructor if i call a method with the Instance of a class that is inherited from the class with the constructor. And i want to fill the list right after I start the project. `public class ConcreteAlertSubject : AlertSubjectBase { private static ConcreteAlertSubject _instance; public static ConcreteAlertSubject Instance { get { return _instance ?? (_instance = new ConcreteAlertSubject()); } } }` – Jeffrey Nov 26 '13 at 15:01
  • The Instance has to be alive before I can do other stuff. I hope you understand it. – Jeffrey Nov 26 '13 at 15:05

1 Answers1

2

You probably want to call the code directly in your global.asax . If you aren't outputting any data to the page, why use ajax?

Will
  • 367
  • 3
  • 4
  • Using this method will also prevent odd race conditions since the `Application_Start` event is only fired once. – Steven V Nov 26 '13 at 14:51
  • Ok thanks. But if I close the project and start it right after it then it doesn't do the method `Application_Start()`. Do you know why? – Jeffrey Nov 26 '13 at 14:57