微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 如何持久保存用户选择(例如:主题选择)

抱歉这个含糊不清的问题但是我走了.

在每个页面上,a都有一个显示不同主题选项的局部视图.这些主题只是页面中元素颜色的不同css类.如何允许任何访问者选择不同的主题,并使其在所有后续请求和会话中保持不变,即使用户登录也是如此.

我想这必须在客户端完成,而我能想到的就像饼干一样.不幸的是,我还没有真正在ASP.NET中对它们进行过实验,并且因为它的基本概念,因此无法想到Google搜索的正确措辞.

如果有人能指出我正确的方向,我会很感激.谢谢

解决方法

您可以使用称为 Profile的概念

使用配置文件,您可以声明要向用户公开的属性,这适用于匿名用户

基本上,配置文件属性存储在cookie中,因此您可以配置它们何时到期以及其他与cookie相关的设置

您的配置文件属性在ASP.Net中作为top-level items compilation – AKA Compilation Life-Cycle的一部分进行编译,因此它们将通过Profile类公开为强类型属性

例如:

Web.config设置

<configuration>
  <system.web>
    <anonymousidentification enabled="true"/>
    <profile defaultProvider="AspNetsqlProfileProvider" enabled="true">
      <properties>
        <add name="FirstName"/>
        <add name="LastName"/>
        <add allowAnonymous="true" name="LastVisit" type="System.Nullable`1[System.DateTime]"/>
        <group name="Address">
          <add name="Street"/>
          <add name="PC"/>
          <add name="InternalNumber" type="system.int32" defaultValue="0"/>
        </group>
        <add name="EmployeeInfo" serializeAs="Binary" type="EmployeeInfo"/>
      </properties>
    </profile>
  </system.web>
</configuration>

代码中使用配置文件(本例中为Global.asax)

void Application_EndRequest(object sender,EventArgs e)
{
    if (Profile != null)
    {
        Profile.LastVisit = DateTime.Now;
        Profile.Save();
    }
}

此外,ASP.Net允许您使用Microsoft AJAX组件访问JavaScript中的属性

Web.config文件

<configuration>
  <system.web.extensions>
    <scripting>
      <webServices>
        <profileService enabled="true" readAccessproperties="LastVisit" writeAccessproperties="LastVisit"/>
        <jsonSerialization maxJsonLength="102400" recursionLimit="100" />
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

ASPX

<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#profile").click(function () {
            Sys.Services.ProfileService.load();
            Sys.Services.ProfileService.properties.LastVisit = new Date();
            Sys.Services.ProfileService.save(
                null,function (m) {
                    alert(m);
                },function (e) {
                    alert(e);
                },null
            );
        });

        Sys.Services.ProfileService.load(null,function (r) {
            $("#res").append("<br/>");
            $("#res").append(Sys.Services.ProfileService.properties.LastVisit.toString());
            $("#res").append("<br/>");
        },function (m) {
            $("#res").append(m.get_message());
        },null);

    });
</script>

<asp:ScriptManager runat="server" ID="sm">
    <AuthenticationService />
    <ProfileService />
    <RoleService />
</asp:ScriptManager>

原文地址:https://www.jb51.cc/aspnet/251061.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐