C# 字典中不存在给定的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18458018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:15:36  来源:igfitidea点击:

given key was not present in the dictionary

c#

提问by muybn

I inherited a project that gives users the above error while trying to retrieve lost password in a .Net 4.0 project. I stepped through and found the trouble spot, but the problem is, the values generated seem right so I don't why the error is occurring. Thanks in advance to anyone who can look at the following code and help me find out how to fix it. Let me know if more information is needed. I looked through everything I could find but nothing gave me clues I could use. Problem is that I just can't trace where the key/value combination should enter, nor would I know how to add it once I did. Code follows.

我继承了一个项目,该项目在尝试检索 .Net 4.0 项目中丢失的密码时给用户带来了上述错误。我一步一步找到了问题所在,但问题是,生成的值似乎是正确的,所以我不知道为什么会发生错误。在此先感谢任何可以查看以下代码并帮助我找出解决方法的人。如果需要更多信息,请告诉我。我浏览了所有我能找到的东西,但没有任何东西给我可以使用的线索。问题是我无法追踪键/值组合应该输入的位置,我也不知道如何添加它。代码如下。

I posted a similar discussion at http://forums.asp.net/t/1926444.aspx/1?given+key+was+not+present+in+the+dictionarybut no one there knew how to help me.

我在http://forums.asp.net/t/1926444.aspx/1?given+key+was+not+present+in+the+dictionary 上发布了类似的讨论,但没有人知道如何帮助我。

void EmailUser(User user)
{
    user.ChangePasswordID = Guid.NewGuid();
    user.Save();
    MailMessage email = new MailMessage();
    //problem line below
    email.From = new MailAddress(Settings.LostPasswordEmailFrom);
    email.To.Add(new MailAddress(uxEmail.Text));
    email.Subject = Settings.LostPasswordSubject;
    email.Body = EmailTemplateService.HtmlMessageBody(EmailTemplates.MembershipPasswordRecovery, new { Body = Settings.LostPasswordText, BeginRequired = "", EndRequired = "", UserName = user.Name, GUID = user.ChangePasswordID.ToString() });
    email.IsBodyHtml = true;
    SmtpClient client = new SmtpClient();
    client.Send(email);

    uxSuccessPH.Visible = true;
    uxQuestionPanel.Visible = false;
    uxUserInfoPanel.Visible = false;
    uxUserNameLabelSuccess.Text = uxEmail.Text;
}

/// <summary>
/// The address that the lost password email will be sent from
/// </summary>
public static string LostPasswordEmailFrom
{
    get
    {
        if (String.IsNullOrEmpty(SiteSettings.GetSettingKeyValuePair()["LA_MembershipProvider_lostPasswordEmailFrom"]))
            return Globals.Settings.FromEmail;
        return SiteSettings.GetSettingKeyValuePair()["LA_MembershipProvider_lostPasswordEmailFrom"];
    }
}

采纳答案by Damith

when you call SiteSettings.GetSettingKeyValuePair()["LA_MembershipProvider_lostPasswordEmailFrom"])if there is no key named LA_MembershipProvider_lostPasswordEmailFromyou will get "The given key was not present in the dictionary"error. What you can do is, check the whether key exist first and then get the value. You can do as below

当您调用时, SiteSettings.GetSettingKeyValuePair()["LA_MembershipProvider_lostPasswordEmailFrom"])如果没有命名的键,LA_MembershipProvider_lostPasswordEmailFrom您将收到“字典中不存在给定键”错误。您可以做的是,先检查键是否存在,然后获取值。你可以做如下

public static string LostPasswordEmailFrom
{
    get
    { 
        var kvp = SiteSettings.GetSettingKeyValuePair();

        if (kvp == null || !kvp.ContainsKey("LA_MembershipProvider_lostPasswordEmailFrom"))
            return Globals.Settings.FromEmail;
        return kvp["LA_MembershipProvider_lostPasswordEmailFrom"];
    }
}