0

I have Existing MVC Web Application url physically not exists (i.e. MyMVCSite/mypage.aspx. i need to redirect page on "error page" if invalid aspx enter by user, which not working in .aspx page condition but when unvalid action enter its work

-) MVCSite/InvalidePage --> Redirect to error Page MVCSite/error

-) MVCSite/InvalidePage.aspx --> Redirect to HomePage as Page MVCSite/InvalidePage.aspx

I need last condition also redirects to Page MVCSite/error So this condition is also tried by me as URL physically not exist its also not work here...

protected void Application_BeginRequest(object sender, EventArgs e)
{ 
    if (sUrl.EndsWith(".aspx"))
    { 
        string[] path = sUrl.Split('/');
        if (!System.IO.File.Exists(Server.MapPath("test.aspx")))
        Response.Redirect("error");
    } 
}

Furthermore i am unable to apply 404 exception in Application_Error event in Global.asax, this exception occurs many time so there is also a checkd of 404 - File does not exist due to some unknow reasons may be some images, css file not find which currently very hard to find

protected void Application_Error()
{
    if (objException.Message != "File does not exist.") { //..... } 
}

I am also apply custom errors in Web.config which also not work

<customErrors mode="Off">
    <error statusCode="404" redirect="Error/Index" />
</customErrors>

Currently error page only occur when action name is wrong but if page name is wrong it redirect us on home page with wrong url Please advice many any other option using that i would resolve this issue

4

2 回答 2

2

看看这个链接,这肯定会有所帮助

看看http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/

url 说 mvc-2 但所有版本都相似

这也是

http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx

用于处理 Global.asax 文件

您可以直接重定向到您的控制器/操作并通过查询字符串传递信息,而不是为此创建新路由。例如:

protected void Application_Error(object sender, EventArgs e) {
  Exception exception = Server.GetLastError();
  Response.Clear();

  HttpException httpException = exception as HttpException;

  if (httpException != null) {
    string action;

    switch (httpException.GetHttpCode()) {
      case 404:
        // page not found
        action = "HttpError404";
        break;
      case 500:
        // server error
        action = "HttpError500";
        break;
      default:
        action = "General";
        break;
      }

      // clear error on server
      Server.ClearError();

      Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
    }

然后您的控制器将收到您想要的任何内容:

// GET: /Error/HttpError404
public ActionResult HttpError404(string message) {
   return View("SomeView", message);
}

您的方法需要权衡取舍。在这种错误处理中循环时要非常小心。另一件事是,由于您正在通过 asp.net 管道处理 404,因此您将为所有这些命中创建一个会话对象。对于频繁使用的系统,这可能是一个问题(性能)。

GlobalASAX 解决方案来源

于 2013-10-11T05:30:27.340 回答
0

您当前已关闭“customErrors”,应将其打开。

<customErrors mode="Off">
  <error statusCode="404" redirect="Error/Index" />
</customErrors>

另外另一种重定向方法是检查控制器代码中的响应是否不等于 null,示例如下:

if (userId == null)
{
  return RedirectToAction("Error404", "Error");
}
else
{
  //Process the request
}
于 2013-10-11T09:44:00.087 回答