Asp.net core TempData中的更多消息 目前我处于一个项目的中间,我想从控制器发送成功/警告/错误/信息消息。

Asp.net core TempData中的更多消息 目前我处于一个项目的中间,我想从控制器发送成功/警告/错误/信息消息。,asp.net-core,tempdata,Asp.net Core,Tempdata,这是我的代码: public static void AddNotification(this Controller controller, string messageType, string message) { ICollection<string> messages = controller.TempData[NotificationsKey] as ICollection<string>; if (messages ==

这是我的代码:

public static void AddNotification(this Controller controller, string messageType, string message)
    {
        ICollection<string> messages = controller.TempData[NotificationsKey] as ICollection<string>;
        if (messages == null)
            controller.TempData[NotificationsKey] = (messages = new HashSet<string>());
        KeyValuePair<string,string> keyValuePair = new KeyValuePair<string, string> (messageType, message);
        //            messages.Add(keyValuePairs);
        string serializedObject = JsonConvert.SerializeObject(keyValuePair, Formatting.None);
        try
        {
            messages.Add(serializedObject);
        }
        catch { }
    }
publicstaticvoidaddnotification(此控制器控制器、stringmessagetype、stringmessage)
{
ICollection messages=controller.TempData[NotificationsKey]作为ICollection;
如果(消息==null)
controller.TempData[NotificationsKey]=(messages=newhashset());
KeyValuePair KeyValuePair=新的KeyValuePair(消息类型,消息);
//添加(keyValuePairs);
string serializedObject=JsonConvert.SerializeObject(keyValuePair,Formatting.None);
尝试
{
messages.Add(序列化对象);
}
捕获{}
}
(仅供参考-我在上面找到的代码)

我的问题是,如果我想添加第二条消息,则会发生错误:

处理请求时发生未处理的异常。 NotSupportedException:集合的大小是固定的。 System.SZArrayHelper.Add(T值)

你能帮帮我吗?如何解决这个问题


非常感谢。

您的意思是什么
添加第二条消息
,错误发生在哪里?我试过你上面的代码,基于SO链接,一切都很好…你能解释一下重现问题的步骤吗?我的意思是当一条消息已经在消息中时(即,成功消息,未显示该用户登录=>它仍在TempData中,我想在那里添加另一条消息。然后它因上面的错误而崩溃。之后,我通过将ICollection类型与List类型交换解决了此问题。。。