2

我在 Application_Start 的某个时候有以下代码

MembershipUserCollection users = Membership.GetAllUsers();

foreach (MembershipUser u in users)
{
    ProfileBase profile = ProfileBase.Create(u.UserName, false);

    if (profile["Index"] != null)
    {
        // Some code
    }
}

问题是,当我尝试访问配置文件属性时,我得到“请求在此上下文中不可用”httpException。我试过使用 profile.GetPropertyValue 但也没有运气。

为什么配置文件需要请求对象,我怎样才能让它工作?

编辑:

我查看了 .NET 代码,发现在某些时候SqlProfileProvider.GetPropertyValuesFromDatabase(string userName, SettingsPropertyValueCollection svc)被调用,其中包含以下代码字符串:

if (!current.Request.IsAuthenticated)

显然,如果不在请求上下文中,这将永远不会起作用。Onur 的答案中描述的解决方法可以正常工作。

4

2 回答 2

1

显然,您正在尝试访问在您的情况下不可能的请求对象。

请查看以下链接并尽可能应用其中一种解决方法。 http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart

如果您提供更多信息,我们可以找到另一种方法。

于 2012-07-17T11:27:26.010 回答
0

您可以通过查询 aspnet_Profiles 表并解析字符串来获取“字符串值”属性。

SELECT [UserName]
  ,[PropertyNames]
  ,[PropertyValuesString]
FROM [aspnet_Profile]
LEFT JOIN [aspnet_Users] on [aspnet_Users].UserId = [aspnet_Profile].UserId

PropertyValuesString 可以用一些代码来解析。此链接应详细说明格式https://msdn.microsoft.com/en-us/library/aa478953.aspx#persistence_format

于 2018-05-30T14:29:57.680 回答