我们有几个c# mvc5
应用。为了集中处理未处理的异常,我希望在主页上有一个自定义错误页面,并在每个应用程序中使用global.asax
(在每个应用程序中)重定向到~Shared/Error
,以防在任何情况下导致未处理的异常我的志趣。
Error.cshtml
添加到了我的母版页共享文件夹中ActionResult
Error()
添加到了SharedController.cs
namespace MasterpageMvc.Controllers
{
[Localizable]
public class SharedController : Controller
{
private SharedViewModel SharedViewModel { get; } = new SharedViewModel();
...
public ActionResult Error()
{
return View();
}
}
}
namespace ApplicationX
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
}
}
}
namespace ApplicationX
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_Error()
{
// Log error to eventlog using NLog
// ...
}
}
}
<system.web>
<customErrors mode="On" defaultRedirect="~/Shared/Error">
<error statusCode="404" redirect="~/Shared/Error" />
</customErrors>
</system.web>
我得到的错误:
System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Error.cshtml
~/Views/Shared/Error.cshtml
~/Views/Shared/Masterpage/Error.cshtml
我们非常感谢您的帮助! TIA acki
答案 0 :(得分:0)
在操作中添加视图名称,以及是否需要母版页名称作为第二个参数
public ActionResult Error()
{
return View("Errors");
}