问题
I'm working on a widgetmanager for asp.net mvc and to get the main part to work, I need to load in a view from outside the default views folder. I got the following folder structure:
... - Views - Widgets |- Views
I need the views to be loaded from inside ~/Widgets/Views I'm not sure if I should be writing a Viewengine from scratch or do something else so please feel free to answer :)
p.S. The widgetmanager is called S3WidgetManager and can be found on GitHub
回答1:
You could write a custom view engine and play around with the following base properties which allow you to customize the location of the views:
base.ViewLocationFormats
base.PartialViewLocationFormats
base.MasterLocationFormats
base.AreaViewLocationFormats
base.AreaPartialViewLocationFormats
base.AreaMasterLocationFormats
And here are their default values:
base.AreaViewLocationFormats = new string[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
base.AreaMasterLocationFormats = new string[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
base.AreaPartialViewLocationFormats = new string[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/{1}/{0}.vbhtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.vbhtml"
};
base.ViewLocationFormats = new string[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
base.MasterLocationFormats = new string[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
base.PartialViewLocationFormats = new string[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml"
};
base.FileExtensions = new string[]
{
"cshtml",
"vbhtml"
};
来源:https://stackoverflow.com/questions/6184021/load-a-view-from-outside-the-views-folder