基于这个问题,在这里和使用代码发现在这里,我试图加载嵌入资源在一个单独的DLL项目的意见,原来的问题的作者说,他已成功这样做-但我不能把它作为工作似乎MVC视图引擎正在拦截请求,仍然在查看视图的文件系统.例外:
Server Error in '/' Application.
The view 'Index' or its master could not be found. The following locations were searched:
~/Views/admin/Index.aspx
~/Views/admin/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/App/Views/admin/Index.aspx
~/App/Views/admin/Index.ascx
~/App/Views/Shared/Index.aspx
~/App/Views/Shared/Index.ascx
Run Code Online (Sandbox Code Playgroud)
我使用的是CustomViewEngine像Rob Connery的/ App结构,如下所示:
public class CustomViewEngine : WebFormViewEngine
{
public CustomViewEngine()
{
MasterLocationFormats = new[] {
"~/App/Views/{1}/{0}.master",
"~/App/Views/Shared/{0}.master"
};
ViewLocationFormats = new[] {
"~/App/Views/{1}/{0}.aspx",
"~/App/Views/{1}/{0}.ascx",
"~/App/Views/Shared/{0}.aspx",
"~/App/Views/Shared/{0}.ascx"
};
PartialViewLocationFormats = ViewLocationFormats;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的路线:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Home", "", new {controller = "Page", action = "Index", id …Run Code Online (Sandbox Code Playgroud) 我正在寻找构建一个Web可配置的布局引擎,并希望直接使用引擎盖下的剃刀引擎.
假设我通过类,xml或数据库提供了一些信息 - 我想在运行时将其转换为本机剃刀代码(我的意思是暗示我定义内容和结构的来源是基于提供者模型).而不是每次请求转换xml文件或其他东西,我想我也可以写Razor,并让视图引擎处理所有事情.
我可以将razor代码从非文件系统的源直接插入引擎吗?
如果我在运行时动态生成.cshtml视图和布局到文件中,并且在控制器动作中动态设置这些视图,那么会不会有任何问题?
是否可以从数据库而不是从磁盘上的文件加载视图?它不一定是数据库,可以是任何字符串.
我想我过早地问过这个问题......我仍然期待任何答案,但我肯定会先做一些研究.
所以我写了一个快速的样本,做了我想要的 - 一点.我会发布更新,因为我让一切正常.
public class DbPathProvider : VirtualPathProvider {
public DbPathProvider() : base() {
}
public override bool FileExists(string virtualPath) {
if (virtualPath.StartsWith("/test") || virtualPath.StartsWith("~/test"))
return true;
return base.FileExists(virtualPath);
//deal with this later
}
public override VirtualFile GetFile(string virtualPath) {
if (virtualPath.StartsWith("/test") || virtualPath.StartsWith("~/test"))
return new DbVirtualFile(virtualPath);
return base.GetFile(virtualPath);
//deal with this later
}
public class DbVirtualFile : System.Web.Hosting.VirtualFile {
public DbVirtualFile(string path) : base (path) {
//deal with this later
}
public override System.IO.Stream Open() { …Run Code Online (Sandbox Code Playgroud) 我要使用Nuts,我正在使用MVCContrib,使用Portable Areas创建可插拔站点,到目前为止一切运行良好,除了当我开始使用MVC模板时,发生的事情是如果我将模板放在相应的文件夹中查看它的工作原理,例子
HostApplication/Views/Home/DisplayTemplates/FirstName.cshtml
HostApplication/Areas/PortableArea_Blog/Views/Home/DisplayTemplates/Auther.cshtml
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是能够创建通用模板从主机应用程序或便携式区域设置和利用它,所以要做到这一点,我创建了一个新的便携式区域称为DisplayTemplates(利用MVCContrib能力编译视图),这里是便携式区域结构
DisplayTemplates
|-Views
|-CommentTemplate.cshtml
Run Code Online (Sandbox Code Playgroud)
现在在我的主机应用程序中我创建了一个测试模型并添加了UIHint属性
public class HostModel
{
[UIHint("~/Areas/DisplayTemplates/Comment.cshtml")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,所以我认为它与部分视图位置有关,所以我创建了一个CustomView引擎来查找该位置的部分视图并在Global.asax中注册它,这里有一个简短的想法,所以我不会厌烦你完整的代码
public class AreaViewEngine : RazorViewEngine
{
public AreaViewEngine()
{
// {0} = View name
// {1} = Controller name
// View locations
ViewLocationFormats = new[]
{
"~/Areas/DisplayTemplates/{0}.cshtml"
};
PartialViewLocationFormats = ViewLocationFormats;
AreaPartialViewLocationFormats = ViewLocationFormats;
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new RazorView(controllerContext, partialPath, null, true, new[] { "cshtml" });
}
protected override IView …Run Code Online (Sandbox Code Playgroud) 我正在从数据库中提取剃刀视图的标记,详情请参阅此问题:
我可以拉取视图,但由于无法识别ViewBag,因此无法执行.
CS0103:当前上下文中不存在名称"ViewBag"
有什么建议?
这是来源:
全球:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new BearForce.Web.Core.DbPathProvider());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)
我的路径提供者:
namespace BearForce.Web.Core
{
public class DbPathProvider : VirtualPathProvider
{
public DbPathProvider()
: base()
{
}
public override bool FileExists(string virtualPath)
{
var repo = new Repository();
var viewPage = repo.GetView(240, virtualPath);
if (base.FileExists(virtualPath))
{
return true;
}
if (viewPage != null)
{
return true;
}
return false;
}
public override VirtualFile GetFile(string virtualPath)
{
if (base.FileExists(virtualPath))
{
return base.GetFile(virtualPath);
}
var …Run Code Online (Sandbox Code Playgroud)