我如何[HandleError]
在asp.net MVC Preview 5中进行过滤?
我在我的Web.config文件中设置了customErrors
<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)
并将[HandleError]放在我的Controller类上面,如下所示:
[HandleError]
public class DSWebsiteController: Controller
{
[snip]
public ActionResult CrashTest()
{
throw new Exception("Oh Noes!");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我让我的控制器从这个类继承并在它们上调用CrashTest().视觉工作室在错误时停止并按下f5继续后,我被重新路由到Error.aspx?aspxerrorpath =/sxi.mvc/CrashTest(其中sxi是所用控制器的名称.当然道路无法找到,我得到"'''应用程序中的服务器错误."404.
这个站点从预览3移植到5.除了错误处理之外,所有东西都运行(没有太多工作要移植).当我创建一个完整的新项目时,错误处理似乎有效.
想法?
- 注意 -
由于这个问题现在有超过3K的视图,我认为放入我目前使用的(ASP.NET MVC 1.0)是有益的.在mvc contrib项目中有一个名为"RescueAttribute"的出色属性你也应该检查一下;)
在两个不同的应用程序中,一个是自定义的另一个是使用新的VS2008 MVC项目获得的示例MVC应用程序,[HandleError]没有捕获异常.
在示例应用程序中,我有:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
throw new Exception();
return View();
}
public ActionResult About()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
这只是默认控制器,抛出异常进行测试.
但它不起作用.它没有进入默认的error.aspx页面,而是在浏览器中显示调试信息.
问题首先出现在我正在处理的自定义应用程序中,这导致我使用示例应用程序对其进行测试.认为它与我在自定义应用程序中所做的更改有关,我将示例应用程序完全保持不变,并在index方法中抛出异常(yuck).
我很难过.我错过了什么?
我在ASP.NET MVC网站中实现了错误处理,就像建议这篇文章一样.
404错误一切正常.但如何正确显示用户友好的屏幕401错误?它们通常不会抛出可在内部处理的异常,Application_Error()
而是返回HttpUnauthorizedResult.一种可能的方法是将以下代码添加到Application_EndRequest()
方法的末尾
if (Context.Response.StatusCode == 401)
{
throw new HttpException(401, "You are not authorised");
// or UserFriendlyErrorRedirect(new HttpException(401, "You are not authorised")), witout exception
}
Run Code Online (Sandbox Code Playgroud)
但在Application_EndRequest()
Context.Session == null中,errorController.Execute()
失败是因为它无法使用默认的TempDataProvider.
// Call target Controller and pass the routeData.
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData)); // Additional information: The SessionStateTempDataProvider requires SessionState to be enabled.
Run Code Online (Sandbox Code Playgroud)
那么,您能否建议一些最佳实践如何在ASP.NET MVC应用程序中使用"用户友好句柄"401?
谢谢.
我相信我已经设置了我们的MVC应用程序以便[HandleError]
正确使用.这是我的控制器:
[HandleError]
public class SupportController : BaseController {
public ActionResult Toss() {
throw new Exception("uh oh");
}
// snip
}
Run Code Online (Sandbox Code Playgroud)
我已将我的customErrors
标记设置web.config
为"开启":
<customErrors mode="On"></customErrors>
Run Code Online (Sandbox Code Playgroud)
但是,我仍然在例外情况下获得黄色死亡屏幕.设置在我的断点Toss()
动作表明,HttpContext.IsCustomErrorEnabled
被设置为true.
我们没有对View Engine做任何事情,BaseController
也没有任何混乱(和其他没有扩展它的控制器有相同的问题).
我在Windows XP上开发,并且当应用程序部署到服务器2003框(IIS 6)时会出现同样的问题.
我认为error.aspx
页面上没有异常:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Sorry, an error occurred while processing your request.
</h2>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
可能有一点不同的是,这个应用程序是在MVC Beta是最新版本时创建的,并且在它们发布时升级到RC然后升级到RTM.可能有一些怪异的设置从那里遗留下来?
我可以在其他应用程序上工作,所以我有点沮丧.
在我的Site.Master文件中,我有3个简单的ViewData参数(我整个解决方案中只有3个参数).这些ViewData值对于我的应用程序中的每个页面都至关重要.由于这些值在我的Site.Master中使用,我创建了一个抽象的SiteController类,它覆盖了OnActionExecuting方法,以便为我的解决方案中的每个控制器上的每个Action方法填充这些值.
[HandleError(ExceptionType=typeof(MyException), View="MyErrorView")]
public abstract class SiteController : Controller
{
protected override void OnActionExecuting(...)
{
ViewData["Theme"] = "BlueTheme";
ViewData["SiteName"] = "Company XYZ Web Portal";
ViewData["HeaderMessage"] = "Some message...";
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当HandleErrorAttribute从SiteController类级别属性启动时,这些值不会传递给MyErrorView(最终是Site.Master).这是一个显示我的问题的简单方案:
public class TestingController : SiteController
{
public ActionResult DoSomething()
{
throw new MyException("pwnd!");
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试通过覆盖SiteController中的OnException()方法来填充ViewData参数,但无济于事.:(
在这种情况下,将ViewData参数传递给Site.Master的最佳方法是什么?
我正在重新访问一些旧代码,并发现它不能与jQuery 1.6一起使用,因为它无法找到$.handleError()
.快速搜索jQuery代码什么都没有,所以我想这个函数已被删除/替换.
有谁知道handleError函数首先缺少哪个版本,以及它是否有直接替换?
我目前有一个继承自的BaseController类System.Web.Mvc.Controller
.在那个类上我有HandleError
属性,将用户重定向到"500 - 糟糕,我们搞砸了"页面.目前正在按预期工作.
这个工作
<HandleError()> _
Public Class BaseController : Inherits System.Web.Mvc.Controller
''# do stuff
End Class
Run Code Online (Sandbox Code Playgroud)
我还有我的404页面在Per-ActionResult的基础上工作,它再次按预期工作.
这个工作
Function Details(ByVal id As Integer) As ActionResult
Dim user As Domain.User = UserService.GetUserByID(id)
If Not user Is Nothing Then
Dim userviewmodel As Domain.UserViewModel = New Domain.UserViewModel(user)
Return View(userviewmodel)
Else
''# Because of RESTful URL's, some people will want to "hunt around"
''# for other users by entering numbers into the address. We need to
''# gracefully redirect …
Run Code Online (Sandbox Code Playgroud) asp.net routing handleerror http-status-code-404 asp.net-mvc-2
我在控制器操作上使用[HandleError]属性时遇到问题 - 它似乎根本不起作用(即过滤器是否存在并不重要 - 我得到相同的结果......).当抛出异常时,我在'/'应用程序错误页面而不是我的自定义视图中得到标准的红色服务器错误.
我在SO上找到了关于这个主题的几个其他线程,在大多数情况下,似乎在web.config中将customErrors选项设置为On解决了这个问题.它不适合我,所以我需要找到一个不同的解决方案.
我的控制器动作:
[HandleError]
public ActionResult Index()
{
throw new Exception("oops...");
return View();
}
Run Code Online (Sandbox Code Playgroud)
在我的web.config文件中
<customErrors mode="On"></customErrors>
Run Code Online (Sandbox Code Playgroud)
我确保Error.aspx文件也在共享目录中.我错过了什么?
我正在运行ASP.NET MVC RC Refresh.
在我的web.config中,我包括:
<customErrors mode="On" />
Run Code Online (Sandbox Code Playgroud)
现在黄色的死亡屏幕不再显示了.我以为我必须将HandleError属性包含在我的控制器方法或类本身中:
[HandleError]
public ActionResult About()
{
throw new Exception("Just an exception");
return View();
}
Run Code Online (Sandbox Code Playgroud)
但它没有任何影响,它是相同的:
public ActionResult About()
{
throw new Exception("Just an exception");
return View();
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下都会显示自定义错误页面.那么HandleError属性又是什么呢?
在我的ASP.NET MVC 2应用程序中,我使用HandleErrorAttribute在未处理的异常情况下显示自定义错误页面,除非在Ajax.ActionLink调用的操作中发生异常,否则它将完美运行.在这种情况下没有任何反应 是否可以使用HandleErrorAttribute用"Error.ascx"局部视图的内容更新目标元素?