在MVC的Web应用程序访问profile.newproperty [英] accessing profile.newproperty in MVC web applications

查看:177
本文介绍了在MVC的Web应用程序访问profile.newproperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我问这个问题<一href=\"http://stackoverflow.com/questions/11706349/how-to-persist-anon-user-selection-ex-theme-selection\">How坚持匿名用户选择(例如:主题选择)。并开始学习ASP.NET配置文件及其在Web配置属性。我试图从链接的答案,但我无法访问profile.newproperty

I recently asked this question How to persist anon user selection (ex: theme selection). and started to learn about ASP.NET profiles and their properties in the web config. I tried the answer from the link but i was unable to access profile.newproperty

如何分配配置文件值?
这个问题指定的Web应用程序不支持框架出来,必须创建基于ProfileBase盒子和一个自定义模式。该问题得到回答在2009年,我想知道这是不是还是一样的情况。

How to assign Profile values? This question specifies that web-applications don't support profile out of the box and a custom model based on ProfileBase must be created. The question was answered in 2009 and I wanted to know if this is still the same case.

在一个ASP.NET 4.0 Web应用程序,我可以访问profile.newproperty与我在web.config中的部分定义,而不需要code C#访问时,除了一个属性。

In a ASP.NET 4.0 web application can I access profile.newproperty with a property i defined in the section in the web.config without needing to code C# except when accessing it.

推荐答案

我刚才看到你的问题,是你是对的,我张贴的答案相关的网站,因此,它不与Web应用程序或MVC工作

I just saw your question, yes you are right, the answer I posted was related to web sites and therefore, it doesn't work with Web Applications or MVC

在这里,我会告诉你code。使用匿名和验证通过的用户配置文件中的MVC与情景工作

Here I will show you code to work with profiles in MVC using anonymous and authenticated user profiles

匿名用户 - 没有配置文件中设置尚未

匿名用户 - 配置文件中设置

验证用户 - 企业信息迁移

<anonymousIdentification enabled="true"/>
<profile inherits="ProfileInWebApplicationMVC1.UserProfile">
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
</profile>

用户配置类

public class UserProfile : ProfileBase
{
    public static UserProfile GetProfile()
    {
        return HttpContext.Current.Profile as UserProfile;
    }

    [SettingsAllowAnonymous(true)]
    public DateTime? LastVisit
    {
        get { return base["LastVisit"] as DateTime?; }
        set { base["LastVisit"] = value; }
    }

    public static UserProfile GetProfile(string userID)
    {
        return ProfileBase.Create(userID) as UserProfile;
    }
}

首页控制器

    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        var p = UserProfile.GetProfile();

        return View(p.LastVisit);
    }

    [HttpPost]
    public ActionResult SaveProfile()
    {
        var p = UserProfile.GetProfile();

        p.LastVisit = DateTime.Now;
        p.Save();

        return RedirectToAction("Index");
    }

索引视图

@if (!this.Model.HasValue)
{
    @: No profile detected
}
else
{
    @this.Model.Value.ToString()
}

@using (Html.BeginForm("SaveProfile", "Home"))
{
    <input type="submit" name="name" value="Save profile" />
}

最后,当你是一名匿名用户的你可以有你自己的空间然而,一旦你注册的网站,你需要的迁移的当前配置文件是新帐户的使用。这是因为ASP.Net会员,创建一个新的配置文件,当用户登录,在

And finally, when you are an anonymous user you can have your own profile however, once you register to the site, you need to migrate your current profile to be used with your new account. This is because ASP.Net membership, creates a new profile when a user logs-in

    public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
    {
        var anonymousProfile = UserProfile.GetProfile(args.AnonymousID);
        var f = UserProfile.GetProfile(); // current logged in user profile

        if (anonymousProfile.LastVisit.HasValue)
        {
            f.LastVisit = anonymousProfile.LastVisit;
            f.Save();
        }

        ProfileManager.DeleteProfile(args.AnonymousID);
        AnonymousIdentificationModule.ClearAnonymousIdentifier();
        Membership.DeleteUser(args.AnonymousID, true);
    }

这篇关于在MVC的Web应用程序访问profile.newproperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆