Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSHTML file outside Views folder

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.

like image 291
Adinosine Avatar asked Oct 15 '25 15:10

Adinosine


1 Answers

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

public PartialViewResult Welcome()
{
    return PartialView("~/Templates/Welcome.cshtml");
}
like image 190
romanoza Avatar answered Oct 17 '25 11:10

romanoza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!