标签: custom-errors

如何在ASP.NET Web应用程序中显示错误消息

我有一个ASP.NET Web应用程序,我想知道如何在抛出异常时显示错误消息框.

例如,

    try
    {
        do something
    }
    catch 
    {
        messagebox.write("error"); 
        //[This isn't the correct syntax, just what I want to achieve]
    }
Run Code Online (Sandbox Code Playgroud)

[消息框显示错误]

谢谢

重复 如何在Web应用程序中显示错误消息框asp.net c#

c# asp.net custom-errors

4
推荐指数
1
解决办法
10万
查看次数

获取对ASP.NET web.config customErrors部分的引用

我正在尝试获取对web.config customErrors部分的引用.当我使用以下代码时,我总是得到一个null.当我得到一个我创建的自定义部分的引用时,我没有这个问题,所以我有点傻眼为什么这不起作用.

CustomErrorsSection customErrorSection =
    ConfigurationManager.GetSection("customErrors") as CustomErrorsSection;
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

CustomErrorsSection customErrorSection = 
    WebConfigurationManager.GetSection("customErrors") as CustomErrorsSection;
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

CustomErrorsSection customErrorSection =
    WebConfigurationManager.GetWebApplicationSection("customErrors") as CustomErrorSection;
Run Code Online (Sandbox Code Playgroud)

编辑:

哎呀!大多数情况都是如此,我在问到这个问题之后就找到了答案.

这对我有用:

System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/");
CustomErrorsSection customErrorsSection = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");
Run Code Online (Sandbox Code Playgroud)

或者更简单地这样:

CustomErrorsSection customErrors = (CustomErrorsSection) WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Run Code Online (Sandbox Code Playgroud)

这也有效:

CustomErrorsSection customErrorsSection = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
Run Code Online (Sandbox Code Playgroud)

所以我想我现在明白了为什么我首先遇到了这个问题.我错误地认为我可以通过尝试GetSection("customErrors")来获取对customErrors部分的引用,但是我没有告诉它它所居住的根部分,并且我的基础是我的尝试基于我知道如何当我没有意识到我的自定义部分是该部分的根时,得到一个自定义部分,所以当我调用GetSection()时,我不必在它前面添加类似system.Web /的东西.

c# asp.net web-config custom-errors

4
推荐指数
1
解决办法
3165
查看次数

覆盖Spring表单错误消息

在Spring中,如何覆盖默认的表单错误按摩?

我正在使用a Validator和属性文件来添加我自己的错误消息,但是如何覆盖在转换​​/编码错误时打印的消息?

它们似乎是自动生成的,我认为对用户没有帮助:

Failed to convert property value of type java.lang.String to required type java.lang.Double for property minPrice; nested exception is java.lang.NumberFormatException: 
Run Code Online (Sandbox Code Playgroud)

java spring default custom-errors

4
推荐指数
1
解决办法
2179
查看次数

我可以告诉 IIS 8.5 在使用 httpErrors 时返回 404 吗?

我在 IIS 8.5 上运行的(Web 窗体)站点上工作,并且目前正在设置 404 和 500 的错误处理。

我已经有了system.web>customErrors设置,但是由于我们在集成模式/较新版本的 IIS 下运行,因此它是从system.webServer> 中提取的httpErrors

使用 IIS GUI,我选择在站点上用静态 HTML 文件替换标准 IIS 错误页面:

<httpErrors errorMode="Custom">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath=""
        path="/problem.html" responseMode="ExecuteURL" />
    <remove statusCode="500" subStatusCode="-1" />
    <error statusCode="500" prefixLanguageFilePath=""
        path="/problem.html" responseMode="ExecuteURL" />
</httpErrors>
Run Code Online (Sandbox Code Playgroud)

不幸的是,虽然显示了正确的错误页面,但不是返回 404,而是返回 200,对于 500 个错误,返回 302 后跟 200。

对于旧版本的 IIS 或未在集成模式下运行的 IIS,您可以redirectMode="ResponseRewrite"customErrors元素上进行设置,这将解决此问题。

我试过设置existingResponsehttpErrors对所有三个可用的选项,以及尝试更新的设置responseMode="File",但这些什么也没有改变。

有没有办法对httpErrrors元素做类似的事情?或者我是否坚持将错误指向 ASP.NET 页面并从那里返回适当的错误? …

asp.net custom-errors integrated-pipeline-mode iis-8.5

4
推荐指数
1
解决办法
1339
查看次数

自定义错误页面无法在实时服务器上运行

我在我的项目中实现了自定义错误功能,它在本地IIS上工作但不在实时服务器上工作.我已经使用Global.asax文件实现了这个功能,我在MVC中的自定义错误控制器中调用我的自定义错误操作方法.我已经发布并运行本地IIS及其工作,但在实时服务器上.

我的Global.asax.cs文件

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    //do not register HandleErrorAttribute. use classic error handling mode
    filters.Add(new HandleErrorAttribute());
}

protected void Application_Error(Object sender, EventArgs e)
{
    LogException(Server.GetLastError());

    CustomErrorsSection customErrorsSection = (CustomErrorsSection)ConfigurationManager.GetSection("system.web/customErrors");
    string defaultRedirect = customErrorsSection.DefaultRedirect;
    if (customErrorsSection.Mod e== CustomErrorsMode.On)
    {
        var ex = Server.GetLastError().GetBaseException();

        Server.ClearError();
        var routeData = new RouteData();
        routeData.Values.Add("controller", "Common");
        routeData.Values.Add("action", "CustomError");

        if (ex is HttpException)
        {
            var httpException = (HttpException)ex;
            var code = httpException.GetHttpCode();
            routeData.Values.Add("status", code);
        }
        else
        {
            routeData.Values.Add("status", 500);
        }

        routeData.Values.Add("error", ex);

        IController errorController …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-mvc custom-errors global-asax

3
推荐指数
1
解决办法
2066
查看次数

无论如何在iis 7上模拟服务器繁忙错误500.13?

有什么办法可以从IIS返回500.13错误吗?我需要测试500.13错误的自定义错误页面.

asp.net iis-7 custom-errors

3
推荐指数
1
解决办法
4468
查看次数

MVC 自定义错误和本地堆栈跟踪的 web.config 中的 &lt;httpErrors&gt; 问题

过去,我习惯于使用web.config 中的customErrors元素来指​​定何时使用自定义错误页面来向用户隐藏详细的异常信息。

从我现在看到的情况来看,最好使用httpErrors。我参考了几个不同的资源,包括 IIS 文档如何完成我需要的,但我似乎无法弄清楚。我需要在 web.config 中指定的要求如下:

1) If HTTP 404 Error
     a) If Local, just return IIS 404 or Yellow Screen of Death (YSOD) or whatever. (It doesn't really matter)
     b) If not local, execute URL /Path/To/My/404/ErrorMVCAction

2) If ANY OTHER HTTP ERROR
     a) Use static error.htm page.
Run Code Online (Sandbox Code Playgroud)

目前,我们有以下几点:

<httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace">
        <remove statusCode="404" />
        <error statusCode="404" 
               prefixLanguageFilePath="" 
               path="/Path/To/My/404/ErrorMVCAction" 
               responseMode="ExecuteURL" />
</httpErrors>
Run Code Online (Sandbox Code Playgroud)

当我导航到一个不存在的页面并得到 404 时,我得到了我的自定义错误页面(我通过将 errorMode 更改为“自定义”来测试了这一点。但是,当出现 500 时,而不是出现黄屏死机在我可以看到本地堆栈跟踪的地方,我得到了蓝色 IIS 错误摘要页面。一个例子如下所示。

在此处输入图片说明

.net iis custom-errors iis-7.5

3
推荐指数
1
解决办法
1152
查看次数

使用Web API将自定义异常序列化为JSON

我需要ModelException在的输出json上显示添加到自定义异常的自定义属性WebApi。所以我创建了如下的自定义异常类

 [Serializable]
public class ModelException : System.Exception
{
    private int exceptionCode;
    public int ExceptionCode
    {
        get
        {
            return exceptionCode;
        }
        set
        {
            exceptionCode = value;
        }
    }

    public ModelException() : base() { }

    public ModelException(string message) : base(message) { }

    public ModelException(string format, params object[] args) : base(string.Format(format, args)) { }

    public ModelException(string message, System.Exception innerException) : base(message, innerException) { }

    public ModelException(string format, System.Exception innerException, params object[] args) : base(string.Format(format, args), innerException) { }


    protected …
Run Code Online (Sandbox Code Playgroud)

c# json custom-errors asp.net-web-api

3
推荐指数
1
解决办法
3221
查看次数

ASP MVC 5-403 customError不起作用

我正在尝试为我的应用程序创建自定义错误页面,它在大多数情况下都起作用,但对于403错误却不起作用。

我的Web.config:

<customErrors mode="On" defaultRedirect="~/Error">
  <error statusCode="404" redirect="~/Error/NotFound" />
  <error statusCode="500" redirect="~Error/InternalServer" />
  <error statusCode="403" redirect="~Error/Forbidden" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

我有一个ErrorController委托这些请求。当404点击,它会显示自定义错误页,但403没有。403 - Forbidden即使为它设置了自定义错误页面,我仍在获取默认的IIS 页面。我环顾四周,尝试使用<httpErrors>它来代替,这似乎每次都给我空白页。

这是我Global.asax的帮助吗:

void Application_Error(object sender, EventArgs e)
{
    Exception exc = Server.GetLastError();

    if (exc is HttpUnhandledException)
    {
        // Pass the error on to the error page.
        Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net custom-errors asp.net-mvc-5

3
推荐指数
1
解决办法
1833
查看次数

如何管理自己创建的错误消息而不是使用默认的庆祝@hapi/joi 代码

我有两个文件,一个是 api.js,另一个是 handler.js。对于模式处理,我正在使用庆祝模块 @hapi/joi

在 api.js 上我只写了 API 名称

在 handler.js 上,我编写了 API 功能。

api.js
//JOI Schema Validator Middleware.
router.use(celebrate({
    body: Joi.object().keys({
        post: Joi.string().max(10),
        userid: Joi.string(),
    })
}));

const handler = require('./handler');

router.post('/createpost', handler.createPost);

router.use(errors());

module.exports = router;
Run Code Online (Sandbox Code Playgroud)

通过这个,如果发生错误,那么我得到了这样的响应

{"statusCode":400,"error":"Bad Request","message":"child \"post\" fails because [\"post\" length must be less than or equal to 10 characters long]","validation":{"source":"body","keys":["post"]}}

我只想将此错误转换为我自己的格式错误,即像这样

{error: true, status: 500, message: 'validation error', version: x.x.2}
Run Code Online (Sandbox Code Playgroud)

默认的 joi 错误是通过router.use(errors());这个模块生成的。我如何修改这个?任何帮助或建议都非常感谢。

custom-errors custom-error-handling hapi joi

3
推荐指数
1
解决办法
3081
查看次数