我有一个带有try..catch
块的私有函数,该函数应该重定向到catch
块中的通用错误页面。问题是,因为如果我这样做,该函数会返回 ViewModel 类型Return RedirectToRoute()
因此存在转换错误。有没有办法从这个函数中重定向到路由,这样我就可以在 catch 块触发时显示错误页面?
如果您的函数是操作结果,那么您可以重定向到另一个操作结果或其他控制器;
public ActionResult asdf()
{
. . .
// return View(model); normally
return RedirectToAction("AnotherAction");
OR
return Redirect("/ControllerName/ActionName?QueryParameter=Value");
OR
string samplecontent = "<h1>For example</h1>";
return Content(samplecontent);
}