Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top