【问题标题】:Razor parsing something that I don't want it to parse. "Parser error"Razor 解析我不希望它解析的东西。 “解析器错误”
【发布时间】:2012-10-10 14:56:58
【问题描述】:

我正在尝试将 Facebook 的评论计数添加到我的网站。我正在使用 Facebook 评论插件,并希望在我的按钮中输出当前计数。但是,我收到来自 Razor 的解析器错误。出于某种原因,Razor 将 -count 结束标记解释为 C# 代码。

“/”应用程序中的服务器错误。

解析器错误

说明:解析服务此请求所需的资源时出错。请查看以下特定的解析错误详细信息并适当地修改您的源文件。

Parser Error Message: Encountered end tag "div" with no matching start tag.  
Are your start/end tags properly balanced?


Source Error: 


Line 42:             </div>
Line 43:         </div>
Line 44:     </div>
Line 45: }

Source File: /Views/Home/Index.cshtml    Line: 44 

Version Information: Microsoft .NET Framework 
Version:4.0.30319; ASP.NET Version:4.0.30319.225

这是我的观点:

@using asdf.WebUI.Helpers
@model IEnumerable<asdf.WebUI.Models.PostModel>

@{
    ViewBag.Title = String.Format("adsf - {0}", DynamicContentHelper.GetDynamicHomePageQuip());
}

@foreach (var post in Model)
{
    <div class="post">
        <a href="@Url.Action("Details", "Post", new { id = post.PostId })">
            <img class="post-img" src="@post.ImageUrl" alt="@post.Description" title="@post.Description" />                 
        </a>        
        <div class="detail">
            <div class="information">
                <h3 class="score">PUNTAJE: <span>234</span></h3>

                <div class="vote">
                    <button type="button" class="btn btn-success"><i class="icon-thumbs-up icon-white"></i> &nbsp; ME GUSTA</button>
                    <button type="button" class="btn btn-danger"><i class="icon-thumbs-down icon-white"></i> &nbsp; NO ME GUSTA</button>
                    <a class="btn btn-info comment-link" href="@Url.Action("Details", "Post", new { id = post.PostId })">

                    <-- ERROR FIRES HERE!!!! -->
                    <i class="icon-comment icon-white"></i> &nbsp; &nbsp; COMENTARIOS &nbsp; &nbsp; <fb:comments-count href="http://asdf.net/bolivia/"/></fb:comments-count> 
                    </a>
                </div>

                <div class="post-description">
                    <blockquote class="pull-right">
                        <p>@post.Description</p>
                        <small>imagen subida por: <a href="@Url.Action("Details", "Post", new { id = post.PostId })">@post.UserName</a></small>
                    </blockquote>
                </div>
            </div>

            <div class="social">
                <a href="#" id="shareFacebook" data-popup-height="380" data-popup-width="660" data-url="http://www.facebook.com/sharer.php?u=http://www.asdf.net/bolivia/@post.PostId">
                    <img src="@Url.Content("~/Public/assets/images/FB-share.png")"/>
                </a>

                <a href="#" id="shareTwitter" data-popup-height="270" data-popup-width="600" data-url="http://twitter.com/intent/tweet?url=http://www.asdf.net/bolivia/@post.PostId&text=@post.Description">
                    <img src="@Url.Content("~/Public/assets/images/TW-share.png")"/>
                </a>
            </div>
        </div>
    </div>
}

【问题讨论】:

标签: c# asp.net-mvc razor


【解决方案1】:

替换这个

  <i class="icon-comment icon-white"></i> &nbsp; &nbsp; COMENTARIOS &nbsp; &nbsp; <fb:comments-count href="http://asdf.net/bolivia/"/></fb:comments-count> 

通过这个

  @Html.Raw(<i class="icon-comment icon-white"></i> &nbsp; &nbsp; COMENTARIOS &nbsp; &nbsp; <fb:comments-count href="http://asdf.net/bolivia/"/></fb:comments-count>)

Razor 不会尝试正常解析它

【讨论】:

    【解决方案2】:

    尝试将所有内容放入 &lt;text&gt;&lt;/text&gt; 块中的每个循环中。

    @foreach (var post in Model)
    {
     <text>
    
    <div class="post"> 
    
    .......
    
    </div>
    
    </text>
    }
    

    还请确保您在 HTML 标记中使用了 facebook xml 命名空间

    <html xmlns:fb="http://ogp.me/ns/fb#">
    

    马修的评论也对。你在 fb 标签中有一个额外的/

    【讨论】:

    • 这里的&lt;text&gt; 并没有太大变化,因为这个意图对剃刀来说已经很明显了。问题中唯一有趣的是额外的/
    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    相关资源
    最近更新 更多