在ASP.net MVC中使用自定义配置文件提供程序?

时间:2009-11-10 16:04:31

标签: asp.net-mvc profile-provider

我目前正在尝试为我的网站实施一个配置文件提供程序几天,并且很难处理它,我是一个php程序员,我最近转移到asp.net

我使用Linq来执行sql并遵循此http://www.codeproject.com/KB/aspnet/LINQCustomProfileProvider.aspx教程。

我使用自己的原因,因为我的结构不同于任何默认的asp.net。配置文件数据位于我的用户表中。

编译很好,登录很好。

但我试过

<% CMSProfile profile = HttpContext.Current.Profile as CMSProfile;%>
<%= profile.NickName %>

它将无法正常工作并抛出System.NullReferenceException ... 那么如何才能自动将我的个人资料放入HTTPCONtext中,这样我每次都可以轻松打电话。

如果您需要更多数据,我可以提供。

非常感谢。

的Web.config:

<roleManager enabled="false" defaultProvider="CMSRoleProvider">
  <providers>
    <clear />
    <add name="CMSRoleProvider" type="P014.ProviderClass.CMSRoleProvider" connectionStringName="P014ConnectionString" applicationName="/" />
  </providers>
</roleManager>

2 个答案:

答案 0 :(得分:2)

您是如何在web.config中注册提供程序的?您不应该自己实例化提供程序,它应该由应用程序在启动时完成。如果您提供更多信息,我可以提供帮助。

编辑:这是我的web.config,也许对你有所帮助。

<profile defaultProvider="SWIntranetProfile" enabled="true">
    <providers>
        <clear/>
        <add name="SWIntranetProfile" type="SWIntranetProfile"/>
    </providers>
    <properties>
        <clear/>
        <!-- SID is really LOGON_USER -->
        <add name="SID" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="PersonID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
        <add name="EmailAddress" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="Position" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="Name" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="FirstName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="LastName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="ImageName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="PhoneExt" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="LastIP" allowAnonymous="false" type="System.String" readOnly="false"/>
        <add name="IntranetTheme" allowAnonymous="false" type="System.String" readOnly="false"/>
        <add name="UnionID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
        <add name="UnionName" allowAnonymous="false" type="System.String" readOnly="true"/>
        <add name="OfficeID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
    </properties>
</profile>

答案 1 :(得分:1)

我注意到文章的作者没有提供将探查器或工作流绑定到HttpContext的代码示例。

你写过自己的课吗?如果是这样,你有没有在web.config中正确设置?

如果你使用IIS7,你还需要在web.config文件的webServer部分注册你的IHttpModule。

修改

为了能够运行您显示的代码片段,您需要将自定义Profiler放置到HttpContext。

您可以通过两种方式执行此操作,可以是基于每个请求,也可以是应用程序启动。

对于每个请求,您需要创建一个实现IHttpModule的类并在web.config中注册它。

对于Application start,您需要将CMSProfile附加到Application_OnStart方法中的HttpContext.Current。

他们是您发布的文章附带的示例应用程序,您下载并检查了示例应用程序吗?