0

我怀疑这仅适用于 asp.net 页面,但据此:

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

我可以像这样定义属性web.config

<profile>
  <properties>
    <add name="PostalCode" />
  </properties>
</profile>

然后继续像这样使用它们:

Profile.PostalCode = txtPostalCode.Text;

但这并不能在 Controller 中为我编译:

public ActionResult Index()
{
  Profile.PostalCode = "codeofpost";
  return View();
}

Profile是类型ProfileBase而不是动态的,所以我不知道这将如何工作,但文件另有说明。

4

2 回答 2

1

Profile 类仅在 ASP.NET 网站项目中生成,而不是在 ASP.NET Web 应用程序中生成。

在 Web 应用程序项目中,您需要使用

ProfielBase.GetPropertyValue(PropertyName);

参考资料:http ://weblogs.asp.net/anasghanem/archive/2008/04/12/the-differences-in-profile-between-web-application-projects-wap-and-website.aspx

于 2013-09-05T13:56:17.677 回答
0

正如我被告知这是不可能的,我决定使用动态来为自己做这件事。我想这最终只是语法糖。

从这里下降使Profile.PostalCode = "codeofpost";

/// <summary>
/// Provides a dynamic Profile.
/// </summary>
public abstract class ControllerBase : Controller
{
    private readonly Lazy<DynamicProfile> _dProfile;

    protected ControllerBase()
    {
        _dProfile = new Lazy<DynamicProfile>(() => new DynamicProfile(base.Profile));
    }

    private sealed class DynamicProfile : DynamicObject
    {
        private readonly ProfileBase _profile;

        public DynamicProfile(ProfileBase profile)
        {
            _profile = profile;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = _profile.GetPropertyValue(binder.Name);
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _profile.SetPropertyValue(binder.Name, value);
            _profile.Save();
            return true;
        }
    }

    /// <summary>
    /// New dynamic profile, can now access the properties as though they are on the Profile,
    /// e.g. Profile.PostCode
    /// </summary>
    protected new dynamic Profile
    {
        get { return _dProfile.Value; }
    }

    /// <summary>
    /// Provides access to the original profile object.
    /// </summary>
    protected ProfileBase ProfileBase
    {
        get { return base.Profile; }
    }
}
于 2013-09-05T14:03:13.080 回答