If I have the controller HomeController
and action Index()
, but I have my template in Views/Index.cshtml
as opposed to Views\Home\Index.cshtml
- is there a way for me to bypass the conventional loading mechanism to render the former?
Asked
Active
Viewed 634 times
0

Alexander Trauzzi
- 7,277
- 13
- 68
- 112
1 Answers
2
Yes, you can explictly tell in the View
method from where to load the view. You just need to start your viewName
parameter with ~/Views
and you also have to write out the .cshtml
extension:
public class HomeController : Controller
{
public ActionResult Index()
{
return View("~/Views/Index.cshtml");
}
}
However the MVC convention is that if you have views which don't belong to one specific controller then these views should go to the Views\Shared folder and from there they will be looked up.

nemesv
- 138,284
- 16
- 416
- 359
-
Tried without ".cshtml" and I got: The view '~/Views/Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Index – Alexander Trauzzi Jan 18 '14 at 22:02
-
It is not working without ".cshtml" extension. You have to use `return View("~/Views/Index.cshtml");` as I've written in my answer. – nemesv Jan 18 '14 at 22:07
-
Got it! This limits me to using razor I assume? Is there any way to do this so that the template resolver can still pick the engine? – Alexander Trauzzi Jan 18 '14 at 22:08
-
It is not the `.cshtml` which matters. But you need to put the extension itself. So if you would use the aspx engine then you need to write `return View("~/Views/Index.aspx");`. If your template comes with a proper `ViewEngine` implementation then this should work. – nemesv Jan 18 '14 at 22:12