Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# global.asax重定向路由不工作_C#_Asp.net Mvc 4_Global Asax_Custom Routes - Fatal编程技术网

C# global.asax重定向路由不工作

C# global.asax重定向路由不工作,c#,asp.net-mvc-4,global-asax,custom-routes,C#,Asp.net Mvc 4,Global Asax,Custom Routes,我想在我的global.asax和response.redirecttoroute中使用自定义路由,但它不起作用。我的RouteConfig中有以下内容: routes.MapRoute( name: "Error", url: "Error/{action}/{excep}", defaults: new { action = "Index", excep = UrlParameter.Optional }

我想在我的global.asax和response.redirecttoroute中使用自定义路由,但它不起作用。我的RouteConfig中有以下内容:

routes.MapRoute(
            name: "Error",
            url: "Error/{action}/{excep}",
            defaults: new { action = "Index", excep = UrlParameter.Optional }
        );
在my global.asax中,我执行以下操作:

Response.RedirectToRoute("Error", new { action="Index", excep=ex.Message });
在我的ErrorController中,我有:

public ActionResult Index(string excep)
    {
        ViewBag.Exception = excep;

        return View();
    }
在错误的索引视图中,我调用ViewBag.Exception来显示异常

当我使用:

Response.Redirect("/Error/Index/0/"+ex.Message, true);
并在我的控制器中使用:

public ActionResult Index(int? id,string excep)
    {
        ViewBag.Exception = excep;

        return View();
    }

它可以工作,但这是默认路线,不是我想要的。为什么它与重定向一起工作,而与redirecttoroute不一起工作?

另一个问题有一个很好的答案:


我会尝试在重定向路由后添加一个
Response.End()
,看看是否有效。

我遇到了同样的问题,但现在我找到了解决方案。也许您可以尝试一下:根据您的需要重命名类名或变量名。更改Global.asax中的任何内容后请记下。请清除浏览器缓存。希望这有帮助

Global.asax

  public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
       //Make sure this route is the first one to be added
        routes.MapRoute(
           "ErrorHandler",
           "ErrorHandler/{action}/{errMsg}",
           new { controller = "ErrorHandler", action = "Index", errMsg=UrlParameter.Optional}
           );
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }
一旦发生未处理异常,将响应从Global.asax应用程序\u错误事件重定向到错误处理程序

 protected void Application_Error(object sender, EventArgs e)
        {
            var errMsg = Server.GetLastError().Message;
            if (string.IsNullOrWhiteSpace(errMsg)) return;
            //Make sure parameter names to be passed is are not equal
            Response.RedirectToRoute("ErrorHandler", new { strErrMsg=errMsg });
            this.Context.ClearError();
        }
错误处理程序控制器

public class ErrorHandlerController : Controller
    {

        public ActionResult Index(string strErrMsg)
        {
            ViewBag.Exception = strErrMsg;
            return View();
        }

    }
要在HomeController的索引ActionResult上测试错误处理程序,请添加此代码。

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //just intentionally add this code so that exception will occur
            int.Parse("test");
            return View();
        }
    }
输出将为


以下是我如何使用MVC 4解决问题的:

RouteConfig.cs

routes.MapRoute(
名称:“ErrorHandler”,
url:“登录/错误/{code}”,
默认值:新建{controller=“Login”,action=“Error”,code=10000}//默认代码为10000
);
routes.MapRoute(
名称:“默认”,
url:“{controller}/{action}/{id}”,
默认值:新建{controller=“Login”,action=“Index”,id=UrlParameter.Optional}
);
Global.asax.cs

受保护的无效应用程序\u Start()
{
//先前代码
RouteConfig.RegisterRoutes(RouteTable.Routes);
this.Error+=Application\u Error;//注册事件
} 
公共无效应用程序\u错误(对象发送方,事件参数e)
{
Exception=Server.GetLastError();
CustomException CustomException=(CustomException)异常;
//你的代码在这里
//在这里,我确信exception变量是CustomException的一个实例。
codeErr=customException.getErrorCode();//从自定义异常获取错误代码
ClearError();
Response.RedirectToRoute(“ErrorHandler”),新
{
代码=代码错误
});
Response.End();
}
技巧如下:确保将Response.End()放在应用程序的末尾\u Error method。否则,重定向到路由将无法正常工作。具体来说,代码参数将不会传递给控制器的操作方法

登录控制器

public class ErrorHandlerController : Controller
    {

        public ActionResult Index(string strErrMsg)
        {
            ViewBag.Exception = strErrMsg;
            return View();
        }

    }
公共类登录控制器:控制器
{
//确保使用与Response.RedirectToRoute方法上传递的路由参数相同的名称命名参数。
公共操作结果错误(整数代码)
{
ViewBag.ErrorCode=代码;
ViewBag.ErrorMessage=EnumUtil.GetDescriptionFromEnumValue((错误)代码);
返回视图();
}
}

您是否尝试清除浏览器缓存?也可以在不同的浏览器中尝试。该解决方案正在发挥作用,我们现在正在使用它。跟踪所有例外情况非常有用。我终于让它工作了。我必须在global.asax中为我的错误路由再次指定控制器和操作。所以我有:Response.redirectoroute(“Error”,new{controller=“Error”,action=“Index”,ex=excep});谢谢你的帮助!