Scuba Steve Scuba Steve - 1 month ago 7
ASP.NET (C#) Question

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
enter image description here

What on earth am I missing here? Why can't Asp.net seem to see views that I create without using the scaffolding system?
Here's the error it throws:
enter image description here

Answer

Your browser will make a request to yourSite/Error/BadFileError and aspnet mvc framework will not directly serve the razor file.

You need to have an action method with that name in your 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.

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. You do not necessarily need to do a redirect(which will issue a new http request).

So instead of the redirect, you can do

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