如何使用asp.net类呈现html代码
我需要能够使用我的类向随机cshtml页面添加动态html代码:如何使用asp.net类呈现html代码,asp.net,html-rendering,dynamic-html,Asp.net,Html Rendering,Dynamic Html,我需要能够使用我的类向随机cshtml页面添加动态html代码: public class HTMLRenderClass { //--here I generate html code public void RenderControll() { LiteralControl ControlGroup = new LiteralControl(); ControlGroup.Text = "<h1>Here will be h
public class HTMLRenderClass
{
//--here I generate html code
public void RenderControll()
{
LiteralControl ControlGroup = new LiteralControl();
ControlGroup.Text = "<h1>Here will be html code generated above</h1>";
Page p = HttpContext.Current.Handler as Page;
if (p != null) p.Controls.Add(ControlGroup);
else { throw new UserFriendlyException("Page still null"); }
}
}
但问题是,我不知道如何将html添加到我需要的页面,因为“HttpContext.Current.Handler as page”总是空的。在这种情况下,我仍然可以使用c.rendercontrol()方法而不传递任何“Page object”参数,有什么解决方案吗?
如果没有,那么我应该向类传递什么来将动态html块放在静态块中?您使用的是Asp.Net MVC,对吗?它不在标记中。您的HTMLRenderClass是否在MVC应用程序中?在MVC中,处理程序通常是“System.Web.MVC.MvcHandler”,而不是“Page”,因此这一行:“Page p=HttpContext.Current.handler as Page;”将始终返回空值。哦,我完全走错了方向。简单的解决方案是:公共字符串RenderControll(){//generate html code return htmlcode;},并在页面上:@html.Raw(c.RenderControll());
@{
HTMLRenderClass c = new HTMLRenderClass();
c.RenderControll();
}