Victor Hugo Terceros Victor Hugo Terceros - 3 years ago 186
jQuery Question

ajaxSetup, between postbacks, MVC Project

I have this function

SetAjaxDefaults: function () {
$.ajaxSetup({
type: 'GET',
headers: General.Headers(),
error: function (data) {
var textLog = '';
if (data && data.responseJSON) {
textLog = data.responseJSON.Message;
} else if (data && data.statusText) {
textLog = data.statusText;
}
}
});
}


I call it in my login page, hoping all my future pages will be affected by this setup, but after some tests I relized that I have to set the defaults for each View or at least for each Layout

Is there a way to set the defaults for the entire site? it would be easy I guess if it were a SPA Framework but it is ASPnet MVC.

Answer Source

The MVC setup includes a shared layout file in Views -> Shared and is called _Layout.cshtml

If you put scripts in here they will be available to all view which use the shared layout.

The layout usually just loads a bundled set of scripts. this is set up in the file in App_Start -> Bundle_Config.cs

If you save your script in a separate file and add it to the bundle, this should be loaded in ALL your layout files.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download