简体   繁体   English

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

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

I am trying to update an Asp.Net web forms website to using MVC 5. There is a shopping cart component that extends the profile provider. 我正在尝试将Asp.Net Web窗体网站更新为使用MVC5。有一个购物车组件可以扩展配置文件提供程序。 This xml located in the web.config appears to auto generate code in the old website project that provides a shopping cart property but does nothing in the mvc version : 位于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>

Hence the following code fails: 因此,以下代码将失败:

Profile.ShoppingCart.AddItem(1500);

Error 3 'System.Web.Profile.ProfileBase' does not contain a definition for 'ShoppingCart' and no extension method 'ShoppingCart' accepting a first argument of type 'System.Web.Profile.ProfileBase' could be found (are you missing a using directive or an assembly reference?) D:\\VirtualWeb2\\VirtualWeb2\\Controllers\\AccountController.cs 92 29 VirtualWeb2 错误3'System.Web.Profile.ProfileBase'不包含'ShoppingCart'的定义,并且找不到扩展方法'ShoppingCart'接受类型为'System.Web.Profile.ProfileBase'的第一个参数(您是否缺少使用指令还是程序集引用?)D:\\ VirtualWeb2 \\ VirtualWeb2 \\ Controllers \\ AccountController.cs 92 29 VirtualWeb2

The original code for the shopping cart is in a stand alone DLL although I can get to the source if I really need to. 购物车的原始代码位于独立的DLL中,尽管如果需要的话我可以直接获取源代码。 I'd prefer to handle it through configuration if this is possible. 如果可能的话,我希望通过配置来处理它。

I've looked at many posts online and can't find anything that covers my same scenario. 我在网上看过很多帖子,找不到任何可以满足我相同情况的东西。 Any idea why this "ShoppingCart" property does not autogenerate when building the mvc project but works in the asp net web forms website? 知道为什么在构建mvc项目时此“ ShoppingCart”属性不能自动生成,但可以在asp net Web窗体网站上工作吗?

KingOfHypocrites answer (from comment): KingOfHypocrites答案(来自评论):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM