迁移 Asp.Net 配置文件提供程序以在 MVC 中工作(扩展无效)

Migrate Asp.Net Profile Provider to Work in MVC (extending not working)

我正在尝试将 Asp.Net 网络表单网站更新为使用 MVC 5。有一个购物车组件可以扩展配置文件提供程序。 xml 位于 web.config 中的这个 xml 似乎在提供购物车的旧网站项目中自动生成代码 属性 但在 mvc 版本中什么都不做:

   <!-- Profile provider -->
    <profile defaultProvider="SQLProfileProvider">
      <providers>
        <clear/>
        <add name="SQLProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </providers>
      <properties>
        <!-- Define our shopping cart which is contained in customerManager and serialized as binary -->
        <add name="ShoppingCart" allowAnonymous="true" type="customerManager.ShoppingCart, customerManager" serializeAs="Binary"></add>
      </properties>
    </profile>

因此以下代码失败:

Profile.ShoppingCart.AddItem(1500);

错误 3 'System.Web.Profile.ProfileBase' 不包含 'ShoppingCart' 的定义并且没有扩展方法 'ShoppingCart' 接受类型 'System.Web.Profile.ProfileBase' 的第一个参数可以是找到(您是否缺少 using 指令或程序集引用?)D:\VirtualWeb2\VirtualWeb2\Controllers\AccountController.cs 92 29 VirtualWeb2

购物车的原始代码位于一个独立的 DLL 中,但如果确实需要,我可以找到源代码。如果可能的话,我更愿意通过配置来处理它。

我在网上看了很多帖子,但找不到任何内容涵盖我的相同情况。 知道为什么这个 "ShoppingCart" 属性 在构建 mvc 项目时不自动生成,但在 asp 网络表单网站上工作吗?

KingOfHypocrites 回答(来自评论):

public static ShoppingCart GetCart(this Controller c) 
{ 
    var cart = c.Profile["ShoppingCart"]; 
    return (customerManager.ShoppingCart)cart; 
}