如果在控制器中没有找到动作,MVC路由到索引?

时间:2022-10-13 16:01:59

I have a multi-page form in my application, and as such, each method posts to the next. This works fine unless you try to visit the URL of one the methods decorated with [HttpPost].

在我的应用程序中,我有一个多页的表单,因此,每个方法都发布到下一个。除非您尝试访问一个使用[HttpPost]修饰的方法的URL,否则这是可以工作的。

Is there any way I can route all 404 requests within this specific controller to the Index method?

有什么方法可以将所有404请求路由到索引方法?

1 个解决方案

#1


1  

I will post this as an answer because I am not able to add it as comment

我将把这个作为一个答案,因为我不能添加它作为评论。

have a look to this link, it might help you

看看这个链接,对你有帮助吗

The idea you can catch the error in the OnActionExecuting and there you can make redirect

您可以捕获onactionexecution中的错误,然后进行重定向

also as mentioned in this page in the answer, you can handle the Controller.OnException

正如在本页面的答案中提到的,您可以处理Controller.OnException

public class BaseController: Controller
{
    protected override void OnException(ExceptionContext filterContext)
    {
        // Bail if we can't do anything; app will crash.
        if (filterContext == null)
            return;
            // since we're handling this, log to elmah

        var ex = filterContext.Exception ?? new Exception("No further information exists.");
        LogException(ex);

        filterContext.ExceptionHandled = true;
        var data = new ErrorPresentation
            {
                ErrorMessage = HttpUtility.HtmlEncode(ex.Message),
                TheException = ex,
                ShowMessage = !(filterContext.Exception == null),
                ShowLink = false
            };
        filterContext.Result = View("Index", data); // to redirect to the index page
    }
}

after this you can let all your controller to inhert from BaseController

在此之后,您可以让所有的控制器从BaseController继承

#1


1  

I will post this as an answer because I am not able to add it as comment

我将把这个作为一个答案,因为我不能添加它作为评论。

have a look to this link, it might help you

看看这个链接,对你有帮助吗

The idea you can catch the error in the OnActionExecuting and there you can make redirect

您可以捕获onactionexecution中的错误,然后进行重定向

also as mentioned in this page in the answer, you can handle the Controller.OnException

正如在本页面的答案中提到的,您可以处理Controller.OnException

public class BaseController: Controller
{
    protected override void OnException(ExceptionContext filterContext)
    {
        // Bail if we can't do anything; app will crash.
        if (filterContext == null)
            return;
            // since we're handling this, log to elmah

        var ex = filterContext.Exception ?? new Exception("No further information exists.");
        LogException(ex);

        filterContext.ExceptionHandled = true;
        var data = new ErrorPresentation
            {
                ErrorMessage = HttpUtility.HtmlEncode(ex.Message),
                TheException = ex,
                ShowMessage = !(filterContext.Exception == null),
                ShowLink = false
            };
        filterContext.Result = View("Index", data); // to redirect to the index page
    }
}

after this you can let all your controller to inhert from BaseController

在此之后,您可以让所有的控制器从BaseController继承