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

发布于 2024-08-10 21:56:21 字数 983 浏览 2 评论 0原文

我目前正在尝试为我的网站实现一个配置文件提供程序几天,但工作起来很困难,我是一名 php 程序员,最近我刚刚转向 asp.net

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

我之所以使用我自己的,是因为我的结构与 ASP.NET 的任何默认结构不同。配置文件数据位于我的用户表内。

编译没问题,登录也没问题。

但我尝试过

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

它不起作用并抛出 System.NullReferenceException... 那么我如何自动将我的个人资料放入 HTTPCONtext 中,以便我每次都可以轻松调用。

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

非常感谢。

网页配置:

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

im currently trying to implement a profile provider for my site a few days now and having a hard time working on it, im a php programer and i just shift to asp.net recently

Im using Linq to sql and follow this http://www.codeproject.com/KB/aspnet/LINQCustomProfileProvider.aspx tutorial.

the reason im using my own because i having different structure than any default of asp.net have. The profile data is inside my user table.

The compile was fine, login was fine.

but i tried

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

it won't work and throw me a System.NullReferenceException...
so how can i automatically get my Profile into the HTTPCONtext so that i can call out easily everytime.

If you need any more data, i can provide.

Thank you very much.

Web.config:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待天淡蓝洁白时 2024-08-17 21:56:22

您如何在 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>

How have you registered the provider in web.config? You shouldn't have to instantiate the provider yourself it should be done by the app at startup. If you give more info I might be able to help.

EDIT: Here is my web.config, maybe it will be of help to you.

<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>
酒儿 2024-08-17 21:56:22

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

您是否编写了自己的类来执行此操作?如果是这样,您是否在 web.config 中正确设置了这一点?

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

编辑

为了能够运行您向您展示的代码片段,您需要将自定义探查器放置到 HttpContext 中。

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

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

对于应用程序启动,您需要将 CMSProfile 附加到 Application_OnStart 方法中的 HttpContext.Current。

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

I noticed that the author of the article didn't give a code sample for binding the profiler or Workflow to the HttpContext.

Have you written your own class to do this? If so, have you set this up correctly in the web.config?

If your using IIS7 you also need to register your IHttpModule under the webServer section of the web.config file.

EDIT

To be able to run the code snippet you have show to you need to have placed your custom Profiler to the HttpContext.

You can do this in two ways, either on a per request basis or on the application start.

For the per request basis you would need to create a class that implements IHttpModule and register it in the web.config.

For the Application start you need to attach your CMSProfile to the HttpContext.Current in the Application_OnStart method.

Their is a sample application attached to the article you posted, have you downloading and checked sample application?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文