【问题标题】:Razor.Parse error during parsing googlemap link解析 googlemap 链接时出现 Razor.Parse 错误
【发布时间】:2018-07-02 21:08:56
【问题描述】:

场景

在我的应用程序中,我使用 Razor.Parse<>(). 将文本、链接和标签解析为 HTML

string Razor.Parse<TEmail>(string razorTemplate, TEmail model) where TEmail : BaseEmail

我使用'@' 解析来自model 的数据。我也解析网页链接。此功能运行良好,直到我解析 Google Map 链接,例如

问题 https://www.google.com/maps/place/New+York,+NY,+USA/@40.6974034,-74.1197634,11z/data=!3m1!4b1!4m5!3m4!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!8m2!3d40.7127753!4d-74.0059728 链接包括“@”。所以我得到错误:

RazorEngine.Templating.TemplateParsingException: '"40.6974034" is not valid at the start of a code block.  Only identifiers, keywords, comments, "(" and "{" are valid.'

我的尝试

我尝试用 dobule @@ 解析 html https://www.google.com/maps/place/New+York,+NY,+USA/@@40.6974034,-74.1197634,11z/data=!3m1!4b1!4m5!3m4!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62!8m2!3d40.7127753!4d-74.0059728 但我得到错误:

RazorEngine.Templating.TemplateParsingException: '"@" is not valid at the start of a code block.  Only identifiers, keywords, comments, "(" and "{" are valid.'

知道我能做什么吗?谢谢你

【问题讨论】:

    标签: c# html parsing razor


    【解决方案1】:

    这是因为您试图传递无效的内容。当您使用“@”关键字时,您不能传递值,您必须传递标识符、关键字、注释、“(”、“{”。“@”关键字标记代码块的开头,例如:

    string value = Razor.Parse("@UserSession.NumberOfPeople")
    

    上面的方法可以,但是这个:

    string value = Razor.Parse("@http://www.something.com")
    

    不起作用,这是无效的。

    您需要做的是找到替代方案。 你需要使用razor engine。如果你不知道它是, 阅读here。您不能转义 @,因为 Razor.parse 不会转义字符串。

    【讨论】:

    • 我知道!我试图通过 dobule 来逃避 @ (@@) 看看 googleMap 链接。当我逃跑时它应该可以工作,但似乎不行。我不知道为什么。
    • 因为 razor.parse 不会转义字符串。
    • 那么怎么做:/?我不能使用 Html.Raw("@") 因为它在 c# 代码中并且 HTML 不可用。
    • 只需使用我上面提供的链接,看看它是否会帮助你:)
    • 我已经安装了 RazorEngine 并尝试使用: htmlBody = Engine.Razor.RunCompile(null, "templateKey", null, email);当然我有命名空间: using RazorEngine.Templating;使用 RazorEngine 但似乎 VisualStudio 没有看到“Engine”
    猜你喜欢
    • 2017-03-19
    • 2015-08-02
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    相关资源
    最近更新 更多