标签: custom-errors

ASP.NET/IIS:404适用于所有文件类型

我在web.config中设置了404处理程序页面,但只有当URL的扩展名为.aspx(或其他由ASP.NET处理)时,它才有效.我知道我可以在网站选项中设置静态HTML页面,但我想要一个页面.是否有任何选项可以为IIS中的所有请求扩展分配ASPX处理程序页面?

asp.net iis web-config custom-errors http-status-code-404

5
推荐指数
1
解决办法
8205
查看次数

将上一个错误传递给自定义错误重定向的最佳方法?

想知道你对这个解决方案的看法,如果这是将错误信息传递给自定义页面的正确方法吗?

在web.config中:

    <customErrors mode="On" defaultRedirect="~/Error.aspx"></customErrors>
Run Code Online (Sandbox Code Playgroud)

在Global.asax中:

<script RunAt="server">
    void Application_Error(object sender, EventArgs e)
    {
    Exception ex = Server.GetLastError();
    if (ex != null && Session != null)
    {
        ex.Data.Add("ErrorTime", DateTime.Now);
        ex.Data.Add("ErrorSession", Session.SessionID);
        HttpContext.Current.Cache["LastError"] = ex;
    }
    }

</script>
Run Code Online (Sandbox Code Playgroud)

在我的Error.aspx.cs中:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack) return;

    if (HttpContext.Current.Cache["LastError"] != null)
    {
        Exception ex = (Exception)HttpContext.Current.Cache["LastError"];
        if (ex.Data["ErrorTime"] != null && ex.Data["ErrorSession"] != null)
            if ((DateTime)ex.Data["ErrorTime"] > DateTime.Now.AddSeconds(-30d) && ex.Data["ErrorSession"].ToString() == Session.SessionID)
                Label1.Text = ex.InnerException.Message;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:我不想从Global.asax做一个Server.Transfer因为..我不知道.对我来说似乎很笨拙.希望能够将customErrors更改为RemoteOnly.所以必须在某处保存最后一个异常,但不能是Session,所以保存到Cache但是有一些额外的数据(时间和SessionID),因为Cache是​​全局的,并且希望确保不向某人显示错误的错误.


我有点改变了我的代码.现在它只是: …

asp.net caching custom-errors getlasterror

5
推荐指数
1
解决办法
7665
查看次数

ASP.NET MVC区域中的自定义错误覆盖

我想要一个MVC区域独有的自定义错误页面.不幸的是,似乎Web.config覆盖系统没有考虑MVC文件夹结构.如果我想覆盖一个名为"mobile"的区域,我必须创建一个名为"mobile"的根项目文件夹(在视图和控制器中),并将Web.config放在那里,并使用新customErrors元素.

有没有更好的方法来执行此操作,以便我不必为任何覆盖创建根文件夹?

asp.net-mvc custom-errors asp.net-mvc-areas asp.net-mvc-2

5
推荐指数
1
解决办法
2197
查看次数

IIS 7.5没有注意到MVC 3应用程序返回的404的customErrors

我正在使用.NET 4.0集成管道应用程序池在IIS 7.5(Win 7 64位)上运行我的MVC 3应用程序(最近从2更新),并在web.config中进行以下设置:

<customErrors mode="On" defaultRedirect="~/Problem/Oops" redirectMode="ResponseRedirect">
    <error statusCode="404" redirect="~/Problem/NotFound" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

如果控制器上的操作方法引发服务器异常并因此生成500错误代码,则它会正确地将浏览器发送到默认重定向URL.

但是,如果我的操作故意通过HttpNotFound()返回一个HttpNotFoundResult,我得到IIS 7.5 404.0错误页面,而不是我的web.config中指示的错误页面.

如果我输入我的应用程序中不存在的URL,例如http:// localhost/MyApp/FOO,那么我会看到web.config指示的页面.

任何人都有任何想法,为什么我在使用HttpNotFound()时没有被重定向到我的自定义404错误页面?

custom-errors iis-7.5 asp.net-mvc-3

5
推荐指数
1
解决办法
1549
查看次数

如何让ASP.NET MVC遵守我的customErrors设置?

在我的web.config中的customErrors标记中,我指向一个控制器.在我的控制器中,我将重定向到由多个应用程序共享的外部错误页面.

<customErrors defaultRedirect="~/Error/ServerError" mode="On">

我的控制器:

public class ErrorController : Controller
{

    public ActionResult ServerError()
    {
        return Redirect("/Systems/ASPNETErrorHandling/ErrorPage.aspx");
    }

    public ActionResult ErrorTest()
    {
        throw new Exception("testing error handling");
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在调用Error/ErrorTest来测试错误处理.但它总是重定向到Views/Shared/Error.cshtml而不是重定向到我指定的控制器.

如何让asp.net mvc遵守customErrors设置中的defaultRedirect路径?

UDPATE:我还使用ELMAH并重写HandleErrorAttribute在描述这个职位.我从.Net Reflector看到,基础HandleErrorAttribute将Error设置为视图.我不认为我可以做很多事情,重定向到Error.cshtml,或其他一些视图.

error-handling asp.net-mvc custom-error-pages custom-errors asp.net-mvc-3

5
推荐指数
1
解决办法
1042
查看次数

如何在c#中创建自定义异常

我想在Windows窗体应用程序中创建自己的异常.我正在尝试将一些数据添加到数据库中.

码:

try
{
    string insertData = string.Format("INSERT INTO " + constants.PIZZABROADCASTTABLE + 
      " VALUES(@starttime,@endtime,@lastupdatetime,@applicationname)");
    sqlCommand = new SqlCommand(insertData, databaseConnectivity.connection);
    sqlCommand.Parameters.AddWithValue("@starttime", broadcastStartDateTime);
    sqlCommand.Parameters.AddWithValue("@endtime", broadcastEndDateTime);
    sqlCommand.Parameters.AddWithValue("@lastuptime", currentDateTime);
    sqlCommand.Parameters.AddWithValue("@applicationname", txtApplicationName.Text);
    sqlCommand.ExecuteNonQuery();
}
catch (DataBaseException ex)
{
    MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)

在这里,我创建了自己的例外.这里我给出了标量变量@lastuptime而不是@lastupdatetime捕获SqlException.

这是我的DatabaseException类.

class DataBaseException : Exception
{
    public DataBaseException(string Message)
        : base(Message)
    {

    }
    public DataBaseException(string message, Exception innerException)
        : base(message, innerException)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

这里运行程序时显示错误

 sqlCommand.ExecuteQuery();
Run Code Online (Sandbox Code Playgroud)

但它不捕获错误并显示消息框文本.我知道我做错了什么.我不知道我创建自定义异常处理是对还是错.

谁能帮我?提前致谢.

c# exception custom-errors

5
推荐指数
1
解决办法
3752
查看次数

自定义错误适用于HttpCode 403但不适用于500?

我正在我的MVC3应用程序中实现自定义错误,它在web.config中打开:

<customErrors mode="On">
  <error statusCode="403" redirect="/Errors/Http403" />
  <error statusCode="500" redirect="/Errors/Http500" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

我的控制器非常简单,具有相应的正确命名视图:

public class ErrorsController : Controller
{
    public ActionResult Http403()
    {
        return View("Http403");
    }

    public ActionResult Http500()
    {
        return View("Http500");
    }
}
Run Code Online (Sandbox Code Playgroud)

为了测试,我在另一个控制器中抛出异常:

public class ThrowingController : Controller
{
    public ActionResult NotAuthorised()
    {
        throw new HttpException(403, "");
    }

    public ActionResult ServerError()
    {
        throw new HttpException(500, "");
    }
}
Run Code Online (Sandbox Code Playgroud)

403工作 - 我被重定向到我的自定义"/错误/ Http403".

500不起作用 - 我被重定向到共享文件夹中的默认错误页面.

有任何想法吗?

custom-errors asp.net-mvc-3

5
推荐指数
1
解决办法
6181
查看次数

配置Magical Unicorn Mvc错误工具包

我试图在我的MVC4网站上配置Magical Unicorn Mvc Error Toolkit(v 2.1.2),但我无法让它工作.这是我的代码:

Web.config文件

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/ServerError">
    <error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" />
</customErrors>

<system.webServer>
   <httpErrors errorMode="Custom" existingResponse="Replace">
     <remove statusCode="404" subStatusCode="-1" />
     <remove statusCode="500" subStatusCode="-1" />
     <error statusCode="404" path="~/Error/NotFound" responseMode="ExecuteURL" />
     <error statusCode="500" path="~/Error/ServerError" responseMode="ExecuteURL" />
   </httpErrors>
<system.webServer>
Run Code Online (Sandbox Code Playgroud)

错误控制器

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View();
    }

    public ActionResult ServerError()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

[这些是基于这个/sf/answers/524958451/帖子]

CustomerErrorHandler.cs(App_Start)

using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using WorldDomination.Web.Mvc;
using CustomErrors.App_Start;

[assembly: WebActivator.PreApplicationStartMethod(typeof(CustomErrorHander), …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc custom-errors asp.net-mvc-4

5
推荐指数
1
解决办法
1066
查看次数

Azure - web.config中的customErrors ="off"未显示Azure应用程序中的详细错误(云服务)

我有一个部署到Azure的应用程序,它使用ADFS(Active Directory联合服务)进行身份验证.

当用户尝试导航到Azure上的应用程序时,它会将用户重定向到ADFS身份验证页面.用户输入凭据并单击"确定",ADFS会将用户重定向到我的应用的登录页面.

到目前为止,一切都很好.一旦用户点击着陆页,我就会在应用上收到通用服务器错误. 问题:我需要查看详细错误. 我尝试设置<customErrors="off" />,重新打包我的应用程序并重新部署,但这不会给我详细的错误:

在此输入图像描述

这是我尝试过的:我尝试在调试模式下打包我的应用程序(在发布模式不起作用后),我编辑了web.config(在解决方案的根目录,以及在Views文件夹中) ,只是为了涵盖所有基地).没有任何效果.

我究竟做错了什么?

web-config azure custom-errors adfs2.0

5
推荐指数
1
解决办法
8497
查看次数

ASP.NET MVC,CustomErrors和ResponseRewrite

我有一个MVC网站(v5,虽然我认为它不相关)我在尝试建立数据库连接时故意引入错误(连接字符串中的服务器IP错误).当用户点击HomeController时,构造函数的一个依赖项是UserRepository(获取当前用户配置文件数据),这取决于数据库连接/会话是否可用.如果不是,则依赖性解析器无法注入UserRepository,并且当发生这种情况时会导致错误(与任何控制器的任何依赖关系一样),并且我得到一个通用的"没有为此对象定义的无参数构造函数".这是没用的.

所以我正在尝试使用自定义错误页面来检索内部异常并以友好的方式显示它.(因为在尝试获取HomeController时发生此错误,它实际上从未到达HandleErrorAttribute,因此依赖于CustomErrors).

所以我有一个带有一系列动作的ErrorsController ......

来自ErrorsComtroller.cs的片段

public ActionResult Error()
{
    return View("Error_500");
}

public ActionResult NotFound()
{
    return View("Error_404");
}
Run Code Online (Sandbox Code Playgroud)

来自web.config的代码段

<customErrors mode="On">
  <error statusCode="404" redirect="~/errors/notfound" />
  <error statusCode="500" redirect="~/errors/error" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

Error_500页面非常基本,它的模型类型为HandleErrorInfo,但如果它不存在,则使用它检查异常详细信息Server.GetLastError().问题是,GetLastError()总是为空,我得到我的自定义错误页面,但没有超出我的"意外错误发生"的一般反馈之外的其他详细信息.在做了一些挖掘后,我发现重定向后该方法不起作用,这是CustomErrors的默认方式.所以我改变了web.config来使用这一行代替......

来自web.config的代码段

这样它就不会导致重定向,并且GetLastError()应该有关于数据库连接问题的异常详细信息.事情是,现在我得到这个消息的默认ASP.NET错误页面.

处理您的请求时发生异常.此外,执行第一个异常的自定义错误页面时发生另一个异常.请求已终止.

所以我使用intellitrace进行了更多挖掘,我看到了有关数据库连接的异常.稍微向下看,我看到HomeController上没有无参数构造函数的错误,然后是一个关于在尝试创建'HomeController'类型的控制器时遇到错误的错误.但后来我看到一个人说

执行/ errors/error的子请求时出错

所以我直接导航到那条路径,页面工作正常.但是当它用于带有ResponseRewriteredirectmode的customerrors时,它会出错.我在动作的第一行(也是唯一一行)上设置了一个断行ErrorsController.Error(),但它永远不会被击中.如果我将自定义错误中的重定向路径替换为静态文件,它可以正常工作,但如果我将其更改回~/errors/error它,则会再次失败.

当指定使用MVC操作作为CustomErrors的url时是否存在问题ResponseRewrite

asp.net error-handling asp.net-mvc custom-errors

5
推荐指数
1
解决办法
1372
查看次数