加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 无法将’System.Web.Profile.DefaultProfile’类型的

发布时间:2020-12-16 10:00:10 所属栏目:asp.Net 来源:网络整理
导读:我看到这个错误: 无法将类型为“System.Web.Profile.DefaultProfile”的对象强制转换为“ProfileCommon”. 在以下代码: ProfileCommon p =(ProfileCommon)ProfileCommon.Create(TextUsername.Text,true); 我不确定为什么…… 解决方法 同意theChrisKent.它
我看到这个错误:

无法将类型为“System.Web.Profile.DefaultProfile”的对象强制转换为“ProfileCommon”.

在以下代码:

ProfileCommon p =(ProfileCommon)ProfileCommon.Create(TextUsername.Text,true);

我不确定为什么……

解决方法

同意theChrisKent.它看起来像web.config中的问题

查看以下示例,如何在asp.net中获取ProfileCommon对象

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GetProfile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        User Name:<asp:TextBox ID="txtUserName" runat="server"/>
        <asp:Button ID="cmdGet" runat="server" OnClick="cmdGet_Click" Text="Get Profile" /><br />
        <asp:Label ID="lbl" runat="server" Text=""/>
    </div>
    </form>
</body>
</html>

文件:Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GetProfile : System.Web.UI.Page
{
  protected void cmdGet_Click(object sender,EventArgs e)
  {
    ProfileCommon profile = Profile.GetProfile(txtUserName.Text);
        if (profile.LastUpdatedDate == DateTime.MinValue)
        {
            lbl.Text = "No user match found.";
        }
        else
        {
            lbl.Text = "This user lives in " + profile.FirstName;
        }
  }
}

文件:Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <profile>
      <properties>
        <add name="FirstName" type="String" serializeAs="Binary"/>
        <add name="LastName" type="String" serializeAs="Xml"/>
        <add name="DateOfBirth" type="DateTime" serializeAs="String"/>
      </properties>
    </profile>
  </system.web>
</configuration>

编辑

此示例仅在项目类型为website时才有效.如果要在Web应用程序中使用ProfileCommon对象,请通过以下链接

Converting Profile Object Code

编辑-II

根据您的评论,以下链接可能会对您有所帮助

ASP.NET Profiles in Web Application Projects

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读