“MembershipUser”类型或命名空间名称找不到。

4

我们最近将解决方案升级到了.NET 4.5。

我了解到System.Web.Security.MembershipUser已经移动到System.Web.ApplicationServices.dll中。

我在web.config文件中添加了对我们网站的引用:

<add assembly="System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

我已经在相关的c#页面中使用了正确的"using"语句:

我已经在相关的c#页面中使用了正确的"using"语句:

using System.Web.ApplicationServices;

而且,以下代码在MembershipUser上仍然会引发错误:
MembershipUser u = Membership.GetUser(User.Identity.Name);

我完全不知道此时该怎么办。我已经研究了一整天并应用了一些更改(主要是web.config文件的更改),但这个c#页面仍然无法识别更改,错误仍然存在。

奇怪的是,即使存在这个错误,该站点仍然可以构建并可用,只有“更改密码”功能无法实现,而这段代码就是为了解决这个问题。

有人能指出我可能缺少引用或程序集的地方吗?


1
你有在项目引用中添加参考吗? - bowlturner
你可能还想在“网络棺材”部分添加一个程序集绑定,将所有低于4.0版本的文件绑定到4.0文件,以确保使用最新的dll。 http://msdn.microsoft.com/zh-cn/library/twy1dw1e(v=vs.110).aspx - Edo Post
@bowlturner 是的,我在解决方案的网站部分经历了“添加引用”对话框,并将该行添加到了Web配置文件中。 - RockiesMagicNumber
@EdoPost 我们有这个:<assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> </dependentAssembly> </assemblyBinding> - RockiesMagicNumber
1
@RockiesMagicNumber 尝试向以下程序集绑定添加以下内容: <dependentAssembly> <assemblyIdentify name="System.Web.ApplicationServices" publicKeyToken="31BF3856AD364E35" /> <bindingRedirect oldVersion="0.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly>这确保了在您的类被移动到新版本中并尝试从旧版本中获取它时,不会引用旧版的 system.web.applicationServices.dll,并且找不到它。 - Edo Post
嗯,遗憾的是,在web.config中添加那行代码并构建网站仍然没有帮助,MembershipUser仍然会抛出错误。 - RockiesMagicNumber
2个回答

2

好的,我想我明白了。问题的一部分似乎在我们的Web.config文件中。

我将c#和vb的编译器版本更改为4.0:

<system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
        <providerOption name="CompilerVersion" value="v4.0"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
        <providerOption name="CompilerVersion" value="v4.0"/>
        <providerOption name="OptionInfer" value="true"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>

此外,我们的编译目标框架没有指定为4.0版本。
<compilation debug="true" batch="false" batchTimeout="7200" targetFramework="4.0">

现在构建网站时,MembershipUser以我预期的方式显示出来了。太棒了。

从2.0升级到4.5并不顺利。


1
我认为这个问题与以下相关:https://dev59.com/E3A75IYBdhLWcg3wZ4Nr - Sergey

0

对于 .NET Framework 4.0 或以上版本,请添加 System.Web.ApplicationServices.dll 的引用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接