3

I have an ASP.NET MVC project and I want to put CSHTML files outside of Views Folder. While I can do so easily by adding the CSHTML files in my desired folder, I am not able to render partial view result from these files.

The folder structure I currently have is roughly as follows: Root | |--Templates | |---Welcome.cshtml | |--Views | |---

I'm using the below syntax from one of the controller actions

    public ActionResult Welcome()
    {
        return PartialView("Welcome");
    }

However, doing so, razor is not able to locate the Welcome.cshtml. I have read about using a custom view engine to change the default search location etc. etc. but wondering if this just can be solved by addition configuration.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Adinosine
  • 33
  • 1
  • 5
  • 1
    You can use a relative path and include the '.cshtml' extension. Look at this post for more info: http://stackoverflow.com/questions/208421/render-partial-from-different-folder-not-shared – David Tansey May 05 '15 at 17:33

1 Answers1

2

You can specify the root-relative path when returning a View/PartialView. For example:

public PartialViewResult Welcome()
{
    return PartialView("~/Templates/Welcome.cshtml");
}
romanoza
  • 4,775
  • 3
  • 27
  • 44
  • 1
    Works like a charm. Just had to include following at the top of my partial cshtml @inherits System.Web.Mvc.WebViewPage – Adinosine May 05 '15 at 18:04
  • That is a good answer, but if you wanted to customize places where MVC looks for views check out this article, it is a bit dated, but it has useful information [Configure The Views Search Location](http://theshravan.net/blog/configure-the-views-search-locations-in-asp-net-mvc/). – CrnaStena May 06 '15 at 00:42