简体   繁体   English

如何动态编译ViewUserControl(Asp.Net MVC)或Asp.Net控件

[英]How to dynamically compile a ViewUserControl(Asp.Net MVC) or Asp.Net Control

I've read the related questions but I cannot find one that suits my problem(or i'm just stupid). 我已经阅读了相关的问题,但是找不到适合我的问题(或者我只是愚蠢)。

Basically i have a factory that renders a certain "template" and that template contains certain "entities". 基本上我有一个工厂,可以呈现某个“模板”,并且该模板包含某些“实体”。 These entities can render themselves and have a void Render method that constructs the HTML and returns it to the template factory. 这些实体可以呈现自己,并具有构造HTML并将其返回到模板工厂的void Render方法。

Now, for maintainability this is a nightmare. 现在,对于可维护性而言,这是一场噩梦。 If the customer wants a space in between the html the dlls would need to be recompiled (Which is terrible!) 如果客户希望在html之间留一个空格,则需要重新编译dll(这很糟糕!)

I need a way to move the logic + HTML out of the void Render method and into some kind of control, parse the control and return HTML at run time. 我需要一种将逻辑+ HTML从void Render方法移出到某种控件中,解析该控件并在运行时返回HTML的方法。 So that the code can be relatively well maintained. 这样就可以相对良好地维护代码。

I'm using Asp.Net MVC and the template factory is a class library. 我正在使用Asp.Net MVC,而模板工厂是一个类库。 Is there any way to accomplish this? 有什么办法可以做到这一点?

Thanks! 谢谢!

Sounds like what you want is a strongly-typed ViewUserControl for each "entity." 听起来像您想要的是每个“实体”的强类型ViewUserControl。 This is basically how I've implemented widgets in a project I'm working on. 基本上,这就是我在正在处理的项目中实现小部件的方式。 Each widget has it's own widget model and a partial view (ViewUserControl) strongly-typed to that model. 每个窗口小部件都有其自己的窗口小部件模型和对该模型强类型化的局部视图(ViewUserControl)。 The widget uses the model's properties to render a view of that widget. 小部件使用模型的属性来呈现该小部件的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM