Load a View from outside the Views folder

别说谁变了你拦得住时间么 提交于 2020-01-25 02:21:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!