asp.net-mvc - 如何从数据库动态加载aspx代码?

标签 asp.net-mvc dynamic dynamic-compilation

我在数据库中存储了这样的内容

<p>This a sample text. <%= Html.ActionLink("test", "myaction", "mycontroller") %></p>

内容是我的数据存储库的一部分,这就是我想将其保留在数据库中的原因。 我想知道如何渲染它并在编译时执行它。

我正在一个 ASP.NET MVC 项目上使用它。

谢谢。

最佳答案

听起来您有一行标记和源代码以字符串形式存储在数据库表的某个位置?

您是否考虑过将数据/代码/值移动到 web.config 中?

考虑将环境配置设置存储在 web.config 中。即

 <appSettings>
   <add Name="IsProduction" value="true" />
   <add Name="RequiresSecure" value="true" />

您的 Controller 和模型可以读取这些值,并将环境设置传递到 View 。

当您写出这些常见问题解答条目时,您可以使用简单的 if 修改输出。

<% if (Model.IsProduction) //have your ViewModel pass along whether you're in Production mode, Dev mode, URLs to have SSL, or whatever criteria you like, etc.
{%>
    <!-- my production markup, with image URL, SSL'd etc. -->
    <img src="https://mysite.com/img.png" />
<%}
else  {%>

    <!-- my other Dev markup, with image URL, etc. -->
    <img src="https://myDevServer/img.png" />
<%} %>

将代码保存在数据库中通常被认为是一种不好的做法,甚至是一种dub-tee-eff。考虑“将数据保存在数据库中”的规则。

关于asp.net-mvc - 如何从数据库动态加载aspx代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2556855/

相关文章:

c# - HttpContext.User.Identity.IsAuthenticated 抛出 System.NullReferenceException : Object reference not set to an instance of an object

asp.net-mvc - Entity Framework 6.2 首次启动非常慢并且 EFInteractiveViews

graph - Raphael JS调整 Canvas 大小

php - 如果 PHP 中的条件动态更改 css

javascript - 动态更改 header 中的元标记?

asp.net-mvc-3 - MEF 和 MVC 3 - 如何从 mef 容器动态加载嵌入 View ?

c# - LINQ- "Unable to create constant value of type [type]"?

asp.net - 部署 MVC,无需关闭整个站点

php - 在运行时在php中动态生成类?

c++ - C/C++ 行号