简体   繁体   English

在aspx文件中将c#代码评估为字符串

[英]Evaluate c# code as string in a aspx file

I have this problem: From a database, held up a string, which contains HTML mixed with C# code. 我有这个问题:从数据库中,举起一个字符串,其中包含与C#代码混合的HTML。 I wish I could run correctly both codes on my page .aspx. 我希望我能在我的页面上正确运行两个代码.aspx。

eg in my .aspx: 例如在我的.aspx中:

<div><%= Model.repo.getCode() %></div>

and the getCode() method give me this: 和getCode()方法给我这个:

<div id="secondDiv"><p><%= Model.Person.Name %></p></div>

so I want the final html file look like: 所以我希望最终的html文件看起来像:

<div><div id="secondDiv"><p>Jhon</p></div></div>

any suggestion? 有什么建议吗?

There may be direct way to bind such value, But if you could store String.Formatable into database then it would be easy to bind the data needed. 可能有直接的方法来绑定这样的值,但如果你可以将String.Formatable存储到数据库中,那么绑定所需的数据将很容易。

Using String.Format you achieve like, 使用String.Format实现像,

returned string from Model.repo.getCode() ( see curly braces ) 从Model.repo.getCode()返回的字符串( 参见花括号

    "<div id="secondDiv"><p>{0}</p></div>"; 

And in ASP code, 在ASP代码中,

    <div><%= string.format(Model.repo.getCode(),Model.Person.Name) %></div>

看一下这个项目,因为它帮助我解决了类似的问题: https//github.com/formosatek/dotliquid基本上你可以将任何对象绑定到模板,该模板可以调用你对象的属性,甚至可以使用条件逻辑和循环。

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

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