System.Web.Mvc.WebViewPage Execute()给出System.NullReferenceException

时间:2019-01-28 13:55:44

标签: asp.net-mvc razor razorengine razorgenerator

以下是从我的Razor视图生成的类的代码:

namespace CompiledRazorTemplates.Dynamic
{
    using System;


    public class RazorEnginClass : System.Web.Mvc.WebViewPage<CustomRazorProcessing.ModelClass>
    {   
        public RazorEnginClass()
        {
        }

        public override void Execute()
        {
            WriteLiteral(" ProcessRazorDemo.ModelClass\r\n");
            int Result = Model.Var1 + Model.Var2;    
            WriteLiteral("\r\n<div");    
            WriteLiteral(" class=\"page-content-wrapper\"");    
            WriteLiteral(">\r\n");    
            WriteLiteral("    ");    
            Write(Result);    
            WriteLiteral("\r\n</div>");    
        }
    }
}

CustomRazorProcessing.ModelClass如下所示:

namespace CustomRazorProcessing
{
    public class ModelClass
    {
        public int Var1 { get; set; }
        public int Var2 { get; set; }
    }
}

现在,我正在创建RazorEnginClass()类的对象,并尝试使用以下代码调用Execute()方法:

ModelClass objModelClass = new ModelClass();
objModelClass.Var1 = 10;
objModelClass.Var2 = 20;

CompiledRazorTemplates.Dynamic.RazorEnginClassobjRazorEnginr = new CompiledRazorTemplates.Dynamic.RazorEnginClass();
objRazorEnginr.ViewData.Model = objModelClass;
objRazorEnginr.Execute();
HttpResponseBase objHttpResponseBase = objRazorEnginr.Response;

但是在“ objRazorEnginr.Execute()”行中,出现以下错误:

An unhandled exception of type 'System.NullReferenceException' occurred in System.Web.WebPages.dll

我无法确定问题所在。

有人可以帮我解决这个问题吗?

谢谢。

0 个答案:

没有答案