我可以将 Web UserControl ascx 和代码存储在数据库中并从那里加载吗?

发布于 2024-11-29 12:06:33 字数 101 浏览 6 评论 0原文

有什么方法可以将 UserControl 的代码存储在数据库表中,动态编译它或从那里加载它?意味着我从数据库中获取一个包含 UserControl 完整代码的字符串,然后将其添加到页面中?

Is there any way I can store the code of the UserControl in a database table, compile it dynamically or Load it from there ? Means I get a string from database that contains the complete code of UserControl and then add it to the page ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-12-06 12:06:33

一些想法:

  1. 使用 UserControl.LoadControl()< /a> 与临时文件
  2. 将 UserControl.LoadControl() 与 HttpHandler 一起使用
  3. 使用 BuildManager.CreateInstanceFromVirtualPath() 包含整个页面 (而不是控件)

选项 1:临时文件(最简单)

  1. 为您的 Web 应用程序创建一个要写入的目录,例如 ~/tmp/ (并给出Web 应用程序修改创建该目录的权限)
  2. 将 UserControl 内容保存到临时文件:

    string userControlContents = /* 从数据库获取用户控件内容 */;
    字符串路径 = Server.MapPath("~/tmp/2011081612332423.ascx");
    System.IO.File.WriteAllText(路径,userControlContents);
    
  3. 加载用户控件:

    Control c = UserControl.LoadControl("~/tmp/2011081612332423.ascx")
    
  4. 将用户控件添加到所需页面:

    this.Controls.Add(c);
    

选项 2:HttpHandler

  1. 实现一个页面或 HttpHandler,通过 id 获取 UserControl 内容,并将数据库中的原始内容输出到响应
    • 这可能不起作用,因为 UserControl.LoadControl() 可能不关心响应流。
  2. 然后按照选项 1 加载控件:

    Control c = UserControl.LoadControl("~/UserControlFromDB.ascx?id=392")
    
    • ASP.NET JIT 缓存可能会中断,因为文件名“UserControlFromDB.ascx”始终相同(仅 id=NNN 发生变化)。 URL 重写可能会解决这个问题。

选项 3:编译页面而不仅仅是控件

BuildManager.CreateInstanceFromVirtualPath() 编译 Page< /code> 在 ASP.NET 中来自虚拟路径。因此,您可以将整个页面存储在数据库中并动态编译它。


免责声明:我不建议在数据库中存储控件或页面;它将增加维护、调试和安全成本。

A few ideas:

  1. Use UserControl.LoadControl() with temp files
  2. Use UserControl.LoadControl() with HttpHandler
  3. Use BuildManager.CreateInstanceFromVirtualPath() with entire pages (instead of controls)

Option 1: temp files (easiest)

  1. Create a directory for your web application to write to, e.g. ~/tmp/ (and give the web application modify & create permissions to that directory)
  2. Save the UserControl contents to a temp file:

    string userControlContents = /* get user control contents from database */;
    string path = Server.MapPath("~/tmp/2011081612332423.ascx");
    System.IO.File.WriteAllText(path, userControlContents);
    
  3. Load the user control:

    Control c = UserControl.LoadControl("~/tmp/2011081612332423.ascx")
    
  4. Add the user control to the desired page:

    this.Controls.Add(c);
    

Option 2: HttpHandler

  1. Implement a page or HttpHandler that gets the UserControl contents by id and outputs the raw contents from the database to the Response stream
    • This might not work since UserControl.LoadControl() probably doesn't care about the Response stream.
  2. Then load the control as in Option 1:

    Control c = UserControl.LoadControl("~/UserControlFromDB.ascx?id=392")
    
    • ASP.NET JIT caching might break since the file name "UserControlFromDB.ascx" is always the same (only id=NNN changes). URL rewriting might work around this.

Option 3: Compiling pages instead of just controls:

BuildManager.CreateInstanceFromVirtualPath() compiles a Page in ASP.NET from a virtual path. So you could store an entire page in the database and compile it dynamically.


Disclaimer: I don't recommend storing controls or pages in the database; it will increase maintenance, debugging, and security costs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文