简体   繁体   English

ASP.NET MVC 5-系统找不到存在的视图

[英]asp.net mvc 5 - system doesn't find a view that exists

So here's the code that's running: 所以这是正在运行的代码:

return RedirectToAction("BadFileError", "Error");

Which then redirects the viewer to: 然后将查看器重定向到:

https://localhost:44340/Error/BadFileError

Which exists in 存在于 在此处输入图片说明

What on earth am I missing here? 我到底在这里想念什么? Why can't Asp.net seem to see views that I create without using the scaffolding system? 为什么不使用脚手架系统,Asp.net似乎看不到我创建的视图? Here's the error it throws: 这是它引发的错误: 在此处输入图片说明

Your browser will make a request to yourSite/Error/BadFileError and aspnet mvc framework will not directly serve the razor file . 您的浏览器将向yourSite/Error/BadFileError发出请求,而aspnet mvc框架将不会直接提供剃刀文件

You need to have an action method with that name in your ErrorController . 您需要在ErrorController有一个具有该名称的操作方法。 The request will be directed to the action method, not to the view file. 该请求将定向到操作方法,而不是视图文件。 Your action method can return that view (or any other view) 您的操作方法可以返回该视图(或任何其他视图)

Add this to the ErrorController. 将此添加到ErrorController。

public ActionResult BadFileError()
{
   return View();
}

Also, If you simply want to show the error view content to the user, you can do that with the View() method call with the full path to your error view. 另外,如果您只想向用户显示错误视图的内容,则可以使用View()方法调用并提供错误视图的完整路径来实现。 You do not necessarily need to do a redirect(which will issue a new http request). 您不一定需要执行重定向(它将发出新的http请求)。

So instead of the redirect, you can do 因此,除了重定向之外,您还可以

return View("~/Views/Error/BadFileError.cshtml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM