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>

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....