gpt4 book ai didi

c# - 如果在 Controller 中找不到操作,MVC 路由到索引?

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:40 24 4
gpt4 key购买 nike

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

有什么方法可以将这个特定 Controller 中的所有 404 请求路由到 Index 方法?

最佳答案

我会将其作为答案发布,因为我无法将其添加为评论

看看这个link , 它可能对你有帮助

您可以在 OnActionExecuting 中捕获错误并在那里进行重定向

也如本page所述在答案中,您可以处理 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
}
}

在此之后你可以让你所有的 Controller 继承自 BaseController

关于c# - 如果在 Controller 中找不到操作,MVC 路由到索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31518283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com