Q:

Setting the CurrentUI in an ASPX project

I am building a Multi-Lingual site in webforms aspx project, and I am thinking of the best solution to do this.
I have some images on the MasterPage, and once clicked, I am calling a Jquery method, that should redirect to a web method.
In turn I have a base page that is initializing the Culture, and all the pages, except for the MasterPage are inheriting from it.
So far I have the following:-
HTML:-
<div class="LocalizationFlags">
<img src="Images/gb.png" onclick="langSelection('gb')" /> &nbsp; &nbsp; 
<img src="Images/fr.png" onclick="langSelection('fr')"/> &nbsp; &nbsp; 
<img src="Images/es.png" onclick="langSelection('es')"/> &nbsp; &nbsp; 
<img src="Images/de.png" onclick="langSelection('de')"/> &nbsp; &nbsp; 
<img src="Images/it.png" onclick="langSelection('it')"/>
</div>

JQuery :-
    function langSelection(lang) {
    setSession(lang);
};

function setSession(Lang) {
    var args = {
        lang: Lang
    };
    $.ajax({
        type: "POST",
        url: "Site.Master.aspx/SetUserCulture",
        data: JSON.stringify(args),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function () {
            alert('Success.');
        },
        error: function () {
            alert("Fail");
        }
    });
};

Site.Master.cs
    [WebMethod(EnableSession = true)]
    private void SetUserCulture(string lang)
    {
        HttpContext.Current.Session["CurrentUI"] = lang;
    }

BasePage.cs
    protected override void InitializeCulture()
    {
        if (Session["CurrentUI"] != null)
        {
            String selectedLanguage = (string)Session["CurrentUI"];
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(selectedLanguage);
        }

        base.InitializeCulture();
    }

Now there are several problems with what I have at the moment.  Jquery does not work, throws a "Fail", and also I know that I cannot use the Site.Master.cs to put the webmethod in.
Is it just a case of creating a WCF service for this method, and then calling it from the Jquery code?
Am I on the right track here?
Thanks for your help and time

A:

Your ajax request most likely fails because it does not reach the destination - as you said you cannot have WebMethod in the master's codebehind. Also there is no need to serialize your parameters with JSON, plain object should work just as well. Besides, dataType: json is not needed here - you do not return any data for this request.
There is no need for a web service here - a simple HTTP handler will suffice. Just do not forget to implement marker interface IRequiresSessionState to have an access to the session inside the handler.
In general cookies might be a better place to store user's culture, since you might want to retain this information across different visits of the same user. Another option is also user profile in the DB - in case you are dealing with registered users. However your current implementation that uses session should work.

