加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

如何在ASP.NET WebService调用中动态初始化文化?

发布时间:2020-12-16 03:26:38 所属栏目:asp.Net 来源:网络整理
导读:任何人都可以告诉我如何在asp.net webservice调用中动态初始化线程文化? 在我的aspx页面中,我有一个基页,我重写了InitializeCulture()方法. 请注意,所选语言的值保存在会话状态中. 解决方法 在Global.asax文件中,您可以设置当前文化,即使其Web服务或网页也
任何人都可以告诉我如何在asp.net webservice调用中动态初始化线程文化?

在我的aspx页面中,我有一个基页,我重写了InitializeCulture()方法.

请注意,所选语言的值保存在会话状态中.

解决方法

在Global.asax文件中,您可以设置当前文化,即使其Web服务或网页也是如此.

// PreRequestHandlerExecute occurs after initialization of Session
void Application_PreRequestHandlerExecute(Object Sender,EventArgs e)
{
    // check if session is required for the request
    // as .css don't require session and accessing session will throw exception
    if (Context.Handler is IRequiresSessionState
        || Context.Handler is IReadOnlySessionState)
    {
        string culture = "en-US";
        if (Session["MyCurrentCulutre"] != null)
        {
            culture = Session["MyCurrentCulutre"] as String;
        }

        System.Threading.Thread.CurrentThread.CurrentCulture = 
           System.Globalization.CultureInfo.CreateSpecificCulture(culture);
    }
}

您正在更改您的要求,但是在Begin_Request方法中无法使用Session对象,您可以在Web方法中执行此操作.

[WebMethod]
public static string MyWebMethod()
{
    String culture = Session["MyCurrentCulutre"] as String;

    System.Threading.Thread.CurrentThread.CurrentCulture = 
       System.Globalization.CultureInfo.CreateSpecificCulture(culture);

    return "My results";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读