代码之家  ›  专栏  ›  技术社区  ›  H. Pauwelyn

`运行时,上下文中不存在html`属性`引擎.Razor.RunCompile`

  •  0
  • H. Pauwelyn  · 技术社区  · 7 年前

    我正在使用SignalR,并将在服务器端呈现一些HTML。为此,我在我的中心使用以下代码:

    protected static string RenderPartialToString(string view, object model)
    {
        string partialViewPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, view.Replace("~/", ""));
        string template = File.ReadAllText(partialViewPath);
        string html = Engine.Razor.RunCompile(template, view, null, model); // exception throwed here
    
        return html;
    }
    

    带参数:

    • view 我的视图的名称和路径。调用方法时的硬编码值: "~/Views/Partials/_programList.cshtml"
    • model : 我从数据库请求得到的数据。

    它给了我一个 RazorEngine.Templating.TemplateCompilationException :

    • 如果问题是关于缺少/无效的引用或多个定义,请尝试加载

      • 尝试其他模型类型(例如 null 使模型动态化)。注意:您不能使用 typeof(dynamic) 使模型动态化!

      • 或者尝试使用静态类型而不是匿名/动态类型。

    有关错误的详细信息:

    • 错误:(103,22)名称 Html 在当前上下文中不存在。

    可在以下位置找到编译的临时文件(请删除文件夹): C:\*****\AppData\Local\Temp\RazorEngine_sof105yt.k4e .

    这就是我的观点:

    @model PaginationList<Program> @* PaginationList<T> inherrits from List<T> *@
    @using WebsitePUC.Models;
    @using WebsitePUC.ViewModels;
    @using WebsitePUC;
    
    @foreach (var program in Model)
    {
        <div class="resultcard row">
            <div class="col-xxs-12 col-sm-9  basicinfo">
                <a href="@program.Url">
                    <h2>@program.Name</h2>
                    @if (program.Description != null)
                    {
                        @Html.Raw(program.Description) @* The error (103, 22) must stand here. *@
                    }
                </a>
                <div class="details">
                    <p>
                        @if (program.Locations.Any())
                        {
                            <strong>Locatie</strong><span>: @string.Join(", ", program.Locations)</span>
                        }
    
                        @if (program.Locations.Any() && program.StartDate.HasValue)
                        {
                            <br/>
                        }
    
                        @if (program.StartDate.HasValue)
                        {
                            <strong>Startdatum</strong><span>: @program.StartDate.Value.ToShortDateString()</span>
                        }
                    </p>
                    <p><a href="inschrijven.shtml" class="btn btn-puc">Schrijf me in</a></p>
                </div>
            </div>
            <div class="extrainfo">
                @if (program.Document != null)
                {
                    <a href="@program.Document.ToString()">
                        <img src="~/uploads/img/uploads/cover.jpg" class="img-fluid">
                    </a>
                }
                else
                {
                    <img src="~/uploads/img/uploads/cover.jpg" class="img-fluid">
                }
                <div class="info">
                    @InfoMaker(program.VideoStream, "opname", "Opname")
                    @InfoMaker(program.VideoConference, "videoconf", "Videoconferentie")
                    @InfoMaker(program.LiveStream, "livestream", "Livestream")
                </div>
            </div>
        </div>
    }
    
    @helper InfoMaker(bool condition, string icon, string name)
    { 
        if (condition)
        {
            <span class="item"><span class="ico @icon"></span><span class="descr">@name</span></span>
        }
    }
    

    这是我启动时的代码:

    public override void Configuration(IAppBuilder app)
    {
        base.Configuration(app); // Needed to load the stuff of the CMS.
    
        HubConfiguration hubConfiguration = new HubConfiguration();
        hubConfiguration.EnableDetailedErrors = true;
        app.MapSignalR(hubConfiguration);
    }
    

    我也试过这个,但也有同样的例外:

    public override void Configuration(IAppBuilder app)
    {
        base.Configuration(app);
    
        GlobalHost.DependencyResolver = new DefaultDependencyResolver();
        HubConfiguration hubConfiguration = new HubConfiguration()
        {
            EnableDetailedErrors = true,
            Resolver = GlobalHost.DependencyResolver
        };
    
        app.MapSignalR(hubConfiguration);
    }
    

    我也试过了 typeof(ProgramList<Program>) 无效的 在第一个代码块中。

    我已经访问了异常消息中的链接,但这对我没有帮助。

    0 回复  |  直到 7 年前