ASP.NET MVC从数据库加载Razor视图

时间:2022-12-04 00:34:50

ScottGu mentioned that we should be able to load a Razor view from a database (check the comments section), so does anybody have an example on how to do that?

ScottGu提到我们应该能够从数据库加载一个Razor视图(查看评论部分),那么有没有人有一个如何做到这一点的例子?

Thanks.

谢谢。

1 个解决方案

#1


30  

You might want to check Pulling a View from a database rather than a file or Using VirtualPathProvider to load ASP.NET MVC views from DLLs

您可能想要检查从数据库而不是文件中拉取视图或使用VirtualPathProvider从DLL加载ASP.NET MVC视图

Taking the code from my previous question on the subject.

从我之前关于这个主题的问题中获取代码。

In your FileExists() method on the other page you replace the test code I have there with some db code that actually checks to see if the virtualPath has an entry in your database. Your database would look something like:

在另一个页面上的FileExists()方法中,用一些db代码替换我在那里的测试代码,这些代码实际上检查虚拟路径是否在数据库中有一个条目。您的数据库看起来像:

Views --tablename
    Path --view's virtual path
    SomeOtherValue

...and your call would then be something like

......然后你的电话就像是

public class DbPathProvider : VirtualPathProvider {
    public DbPathProvider() : base() {

    }

    public override bool FileExists(string virtualPath) {
        Database db = new Database();
        return db.Views.Any(w => w.Path == virtualPath);
    }

    public override VirtualFile GetFile(string virtualPath) {
        return new DbVirtualFile(virtualPath);
    }
}

And now we modify the DbVirtualFile

现在我们修改DbVirtualFile

public class DbVirtualFile : System.Web.Hosting.VirtualFile {

    public DbVirtualFile(string path) : base (path) {

    }

    public override System.IO.Stream Open() {
        Database db = new Database();
        return new System.IO.MemoryStream(
                   db.Views.Single(v => v.Path == this.VirtualPath));
    }
}

The virtualPath doesn't have to correspond to a real filesystem if you don't want it to. You can override the functionality by implementing these two classes.

如果您不想要虚拟路径,则虚拟路径不必与真实文件系统对应。您可以通过实现这两个类来覆盖功能。

You can then register your new VirtualPathProvider in the global.asax like so

然后,您可以在global.asax中注册新的VirtualPathProvider

HostingEnvironment.RegisterVirtualPathProvider(new DbPathProvider());

I hope this better answers your question.

我希望这能更好地回答你的问题。

#1


30  

You might want to check Pulling a View from a database rather than a file or Using VirtualPathProvider to load ASP.NET MVC views from DLLs

您可能想要检查从数据库而不是文件中拉取视图或使用VirtualPathProvider从DLL加载ASP.NET MVC视图

Taking the code from my previous question on the subject.

从我之前关于这个主题的问题中获取代码。

In your FileExists() method on the other page you replace the test code I have there with some db code that actually checks to see if the virtualPath has an entry in your database. Your database would look something like:

在另一个页面上的FileExists()方法中,用一些db代码替换我在那里的测试代码,这些代码实际上检查虚拟路径是否在数据库中有一个条目。您的数据库看起来像:

Views --tablename
    Path --view's virtual path
    SomeOtherValue

...and your call would then be something like

......然后你的电话就像是

public class DbPathProvider : VirtualPathProvider {
    public DbPathProvider() : base() {

    }

    public override bool FileExists(string virtualPath) {
        Database db = new Database();
        return db.Views.Any(w => w.Path == virtualPath);
    }

    public override VirtualFile GetFile(string virtualPath) {
        return new DbVirtualFile(virtualPath);
    }
}

And now we modify the DbVirtualFile

现在我们修改DbVirtualFile

public class DbVirtualFile : System.Web.Hosting.VirtualFile {

    public DbVirtualFile(string path) : base (path) {

    }

    public override System.IO.Stream Open() {
        Database db = new Database();
        return new System.IO.MemoryStream(
                   db.Views.Single(v => v.Path == this.VirtualPath));
    }
}

The virtualPath doesn't have to correspond to a real filesystem if you don't want it to. You can override the functionality by implementing these two classes.

如果您不想要虚拟路径,则虚拟路径不必与真实文件系统对应。您可以通过实现这两个类来覆盖功能。

You can then register your new VirtualPathProvider in the global.asax like so

然后,您可以在global.asax中注册新的VirtualPathProvider

HostingEnvironment.RegisterVirtualPathProvider(new DbPathProvider());

I hope this better answers your question.

我希望这能更好地回答你的问题。