Alexander Trauzzi
Alexander Trauzzi

Reputation: 7396

How do I specify a specific path for a Razor template?

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?

Upvotes: 0

Views: 828

Answers (1)

nemesv
nemesv

Reputation: 139758

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.

Upvotes: 2

Related Questions