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> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 在MVC中写数据库和业务逻辑的位置?
- asp.net-mvc – Knockout,CKEditorSingle Page App
- ASP.NET Health Monitoring和ELMAH是否相互替代?
- asp.net-mvc – 如何在MVC选择路由之前添加路由参数
- asp.net-mvc – 使用Ajax.ActionLink进行正确的HTTP删除问题
- asp.net-mvc-4 – MVC4捆绑GZIP和头文件
- asp.net-mvc – 如何在ASP.NET MVC4中使用具有唯一标识符UR
- ASP.NET编译器在更新到5.2.2之后无法加载System.Web.Mvc
- asp.net-mvc – MVC发布复杂对象的列表
- asp.net-mvc – MVC4 Action返回没有null的JsonResult
- asp.net – Visual Studio 2017 docker错误运行应
- iis – 禁用应用程序池的空闲超时是否有任何危害
- asp.net-mvc – 有没有办法让RoutePrefix以可选参
- asp.net-mvc-4 – 使用KNOCKOUT.JS和ASP.NET MVC
- asp.net – 在MVC 4.0中使用部分视图中的节
- asp.net – 如何使我的硒测试不那么脆弱?
- asp.net-mvc – 具有接受routeValues和htmlAttri
- asp.net – 多租户应用程序的输出缓存,因主机名和
- asp.net-web-api – 使用Owin时无法获取ASP.NET
- asp.net-mvc – 防止在ASP.NET MVC中缓存属性,每
