asp.net - 如果捕获所有路由均不匹配,则显示“找不到404文件” View

标签 asp.net asp.net-mvc error-handling asp.net-mvc-routing

我在ASP.NET MVC中设置了所有路由,因此可以捕获/this-page,/that-page等。

当您点击页面时,将调用操作,说出Index(string page),然后针对数据库中的值测试page以确定是否可以找到该页面。如果找不到它,我想显示在FileNotFound中的 View ErrorController,我不想重定向,因为我想将url保留为http://.../page-that-isnt-found,实际上就像StackOverflow一样,如果您给它一个假问题-(https://stackoverflow.com/questions/zzz-aaa)。

现在我的问题在于弄清楚代码,我已经尝试了两种:

[ do page db check ]
if (page == null)
   return RedirectToAction("FileNotFound", "Error");
   // return View("FileNotFound"); // can't opt for a controller ?

我得到的最接近的是RedirectToAction(),但是它当然会进行实际的重定向,并且我的url指向/error,这是不希望的行为。我尝试使用return View(),但似乎无法指定 Controller ,只能指定 View 名称?

最佳答案

正确,通过返回一个View,您指向的是要呈现的View名称,而不是ActionMethod。您应该能够创建返回的包含404页面内容的 View (在同一 Controller 的 View 或共享 View 中)。

希望这可以帮助。

关于asp.net - 如果捕获所有路由均不匹配,则显示“找不到404文件” View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1943322/

相关文章:

c# - 传入字典的模型项是 'System.Web.Mvc.HandleErrorInfo' 类型的,但是这个字典需要一个模型项的类型

java - "Spring Boot Project"错误 : Could not find or load main class fyp. fms.FleetManagementSystemApplication

laravel - 解析错误: syntax error, unexpected '$kernel' (T_VARIABLE) in the index.php file

mysql - ASP.NET MySQL openauth 不工作

javascript - CasperJS 无法点击 DIV 按钮

asp.net - 使用linq获取网页中某种类型的Web控件列表

c# - 多个arraylist重复subjectname顺序显示

c# - ControllerContext 为空

asp.net-mvc - "Unable to open the database file"(警告 : Wide error)

java - 通过 JTable 从 ArrayList 中删除项目