错误消息的中央页面

viv*_*ous 0 c# asp.net error-handling

在我的ASP.NET C#应用程序中,有许多我想要显示的错误消息.

我显示错误消息的方式是通过以下方式弹出:

       Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('My error message here');", true);
        return;
Run Code Online (Sandbox Code Playgroud)

由于有许多不同的错误消息(一些在页面上重复)并且我的应用程序中有很多不同的页面 - 我想将所有错误消息放在一个集中的页面上并以某种方式引用它 - 这样当我需要时更改我的错误消息,我只需要更改一个页面而不是所有页面.

最好的方法是什么?

我想我需要创建一个.cs页面?并为每个错误消息使用不同的ID.

这似乎是一件非常简单的事情,但我对如何开始它有点迷茫.

有人可以建议最好的方法吗?

谢谢.

Dam*_*ith 6

我会添加扩展方法来显示警报并将所有异常字符串保存在资源文件中.然后我可以调用如下方法

用法

this.ShowAlert(Resource1.MyException);
Run Code Online (Sandbox Code Playgroud)

扩展方法

using System;
using System.Web.UI;

namespace WebApplication1
{
    public static class Extensions
    {
        public static void ShowAlert(this Control control, string message)
        {
            if (!control.Page.ClientScript.IsClientScriptBlockRegistered("msgbox"))
            {
                var script = String.Format("alert('{0}');", message);
                control.Page.ClientScript.RegisterStartupScript(control.Page.GetType(), "msgbox", script, true);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将资源文件添加到项目中,并将消息输入为具有全名的字符串条目.