0

We have a .NET 4.0, MVC 2 project, where the HomeController looks like this:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("~/client/index.html");
    }
 }

All is fine.

But, when we start linking to .NET 4.5 and MVC 4, the runtime can't seem to find this index.html! We get this error:

The view '~/client/index.html' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/client/index.html

How could this be! What might we be missing here.

Liam
  • 27,717
  • 28
  • 128
  • 190
cooper_milton
  • 67
  • 1
  • 12
  • 1
    Why are you returning `html` instead of a razor view `cshtml`? – Erik Philips Sep 19 '17 at 20:36
  • `View()` expects a path to a Razor view (.cshtml). If you don't want to do this you need to return a different method result, see duplicate. I'm guessing MVC 2 was more forgiving in this, but well MVC 2 didn't support Razor at all. – Liam Sep 20 '17 at 10:07
  • Liam, that link above helped resolve loading the HTML issue, thanks! – cooper_milton Sep 20 '17 at 15:36
  • Liam, a new problem, my html has href references like this: . Using FilePathResult, the above "code" gets sent to the client as is and so it blows up! Any idea how to address it? – cooper_milton Sep 20 '17 at 15:49

2 Answers2

1

I've never seen MVC using straight html pages. This is a more typical setup:

Controller

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("Index");
    }
}

The view would be /views/Home/Index.cshtml. By default MVC wants views to be in the views folder in a folder corresponding to the controller name.

If you want to have the layout broken into a separate file it would typically be in /views/Shared/.

If your HTML page is self-contained you should be able to move it and rename it to index.cshtml and add the following somewhere on the page.

@{
    Layout = null;
}
Joshua Morgan
  • 678
  • 6
  • 18
0

Take care when locating your html page. Placing it inside a view folder where, by the MVC rules, a controller would be expected to do the handling, causes errors in my testing. Here's one way to make it work, though this isn't really coding to MVC pattern:

enter image description here

You can then reference the page:

enter image description here

Notice I've stepped outside the controller/view structure (not recommended for MVC) To make it fail, which I'm assuming is similar to what is happening for you:

enter image description here

(notice the html is placed in a view where we'd expect a matching controller method to serve it to a caller)...

But...

enter image description here

So if you insist on going this approach perhaps you can set up a content folder outside your MVC controller/view structure and place your html there. But, again, not to beat the subject to death, you could easily convert this to cshtml and serve it up via a simple method in a controller. Just my two cents' worth..

markaaronky
  • 1,231
  • 12
  • 29
  • You seem to of gone to a lot of effort to demonstrate something that doesn't work? I'm not clear how this helps – Liam Sep 20 '17 at 10:07
  • Mark, thanks for the effort. The link that Liam pointed out above, lets me load the html now. Thanks Liam! – cooper_milton Sep 20 '17 at 15:35
  • Liam, the first part of my post demonstrates what does work, and explains why it works (a controller isn't expected), the second part expands on the answer an earlier user gave (why placing a page inside the traditional view structure then causes MVC to expect a controller to handle it) – markaaronky Sep 27 '17 at 19:55