ASP.NET MVC3中不同区域的自定义错误页面

时间:2011-08-30 12:42:33

标签: c# asp.net-mvc-3 error-handling

我已使用

在我的网站上设置了自定义错误页面
<customErrors mode="RemoteOnly" defaultRedirect="~/Error">
  <error statusCode="500" redirect="~/Error/InternalError"/>
  <error statusCode="404" redirect="~/Error/FileNotFound"/>
  <error statusCode="403" redirect="~/Error/AccessDenied"/>
</customErrors>

但是,网站上还有另一个区域,供应商,当供应商区域发生错误时,重定向将转到Suppliers / Error / _ 。由于我这里没有任何错误页面,网站似乎挂起从不显示错误页面。如何在不必将错误页面复制到供应商区域的情况下解决此问题?

2 个答案:

答案 0 :(得分:1)

据我了解MVC你的URL组成,默认情况下是:

域/控制器/动作/ ID

如果您有“错误”控制器。在您的逻辑中,您测试以查看请求是否源自需要重定向到“供应商”错误页面的站点用户

 [HandleError]
    public ActionResult Index()
    {
        // Test to see if you need to go to the SuppliersController
        if (this.User.IsInRole("supplier"))
        {
            return Redirect("/Suppliers/Error");
        }
        else
        {
            return View(); // This returns the "Error" View from the shared folder
        }
    }

重定向到您的Suppliers Controller上的错误处理操作,该操作将返回正确的视图。

public class SuppliersController : Controller
{
    //
    // GET: /Suppliers/

    public ActionResult Error()
    {
        return View("Error","SomeMasterPage"); // No "Error" view in Suppliers View folder, so it uses the one in shared folder
    }

}

您还可以使用“供应商错误操作”中的[Authorize]属性来确保用户已登录。

通过这种方式,您将获得所需的 / Suppliers / Error URL,并可以使用SuppliersController Action指定所需的视图,模型和主/布局页面。

还要看一下类似问题的这个非常全面的回复:

Best 404 example I can find for mvc

答案 1 :(得分:-1)

我想在错误页面之前移除“〜”应该可以解决问题,但是你需要“\”。

另一种方法是在redirect / defaultRedirect属性中写入FULL URL。