【问题标题】:How to persist anon user selection (ex: theme selection)如何坚持匿名用户选择(例如:主题选择)
【发布时间】:2012-07-27 05:26:28
【问题描述】:

很抱歉这个模棱两可的问题,但我走了。

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

我想这必须在客户端完成,我能想到的只是 cookie 之类的东西。不幸的是,我还没有真正在 ASP.NET 中对它们进行过实验,并且由于它是一个基本概念,所以我无法为 Google 搜索找到合适的措辞。

如果有人能指出我正确的方向,我将不胜感激。谢谢

【问题讨论】:

    标签: asp.net-mvc cookies


    【解决方案1】:

    您可以使用称为 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>
    

    【讨论】:

    • 完美谢谢。这就是我来stackoverflow的原因。通过答案和人总是可以为我指明正确的方向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多