0

I'm currently creating an MVC ASP.NET application in which multiple widgets are loaded from a different location. I have most of the logic down, but I get stuck trying to load the widgets from a differnt location than the original one.

Everything was working when I used this

foreach (WidgetPrototype.Models.Widget widget in Model)
    {
        <blockquote style="border-style: outset">
            @Html.Partial(widget.Name)
        </blockquote>
    }

But when I moved the files to test loading views from a different location, changing my code to

foreach (WidgetPrototype.Models.Widget widget in Model)
    {
        <blockquote style="border-style: outset">
            @Html.Partial(@"D:\" + widget.Name + ".cshtml")
        </blockquote>
    }

It stopped working, and gave the error that the view could not be found, with the message:

[InvalidOperationException: The partial view 'D:\Clock.cshtml' was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Widgets/D:\Clock.cshtml.aspx ~/Views/Widgets/D:\Clock.cshtml.ascx ~/Views/Shared/D:\Clock.cshtml.aspx ~/Views/Shared/D:\Clock.cshtml.ascx ~/Views/Widgets/D:\Clock.cshtml.cshtml ~/Views/Widgets/D:\Clock.cshtml.vbhtml ~/Views/Shared/D:\Clock.cshtml.cshtml ~/Views/Shared/D:\Clock.cshtml.vbhtml]

So apparently it's still trying to find the files on a relative path in the project.

Is there a way to force it to just use the full path?

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • 2
    Unsure if I understood (re: `D:\ `) - paths are relative to _your application_ (not just "any" location in your/server file system). Your app/IIS doesn't have permissions to just access/serve resources from any location in a file system without configuration...Hth. – EdSF Feb 16 '17 at 16:37

1 Answers1

1

Have you tried specifying the relative path? E.g. @Html.Partial("../MyViews/_PartialView", Model) ?

Would also recommend using @Html.RenderPartial as it works faster.

  • I can't use a relative path as the views are on a different drive. – Dennis van Gils Feb 16 '17 at 16:29
  • From what I've researched, you can create your own "custom" `RazorViewEngine` and specify paths to look in. Could it help you? http://stackoverflow.com/questions/9838766/how-do-i-implement-a-custom-razorviewengine-to-find-views-in-non-standard-locati – VladimirBadiuk Feb 16 '17 at 16:42