简体   繁体   English

检测Razor / C#代码?

[英]Detect Razor/C# code?

Is there a way to detect if an HTML page contains any razor/C# code? 有没有一种方法可以检测HTML页面是否包含任何razor / C#代码? Essentially I want users to be able to provide custom layouts, with tags that I will replace with RenderSection. 本质上,我希望用户能够提供自定义布局,并带有将被RenderSection替换的标签。 I want to validate that prior to making this replacement, that none of the HTML contains anything like for example, <a href="@(some C# code)" . 我想验证一下,在进行替换之前,没有HTML包含类似<a href="@(some C# code)"

All discussions about alternative ways to do this, should/could/would aside, just simply: Is there a way to programmatically detect if a file contains C#/Razor code? 所有关于替代方法的讨论都应该/可以/应该简单地抛在一边:是否有一种方法可以以编程方式检测文件是否包含C#/ Razor代码?

I don't know a lot about the Razor markup -- but I am thinking that when you grab the layout string they are passing in you will want to parse the text out and grab everything that starts with an @ and toss those words into an array. 我对Razor标记了解不多-但我想当您抓取布局字符串时,他们想要传入的内容就是解析文本并抓取所有以@开头的单词,然后将这些单词扔进一个数组。 Then, when you republish it to you website use razor code to access the data in the array... 然后,当您将其重新发布到网站时,请使用剃刀代码访问数组中的数据...

Alternately, and easier, would be to go through all the passed in code and replace all the @ signs with a different symbol say & that way it wont get interpreted by the Razor processor: 或者,更容易地,是遍历所有传入的代码并将所有@符号替换为不同的符号say & ,这样它就不会被Razor处理器解释:

layoutString = layoutString.Replace('@', '&');

In the browser? 在浏览器中? No, because unless the programmer made a mistake, there is no Razor/C# code in teh rendered HTML, only HTML that was the result of that. 不可以,因为除非程序员犯了错误,否则呈现的HTML中没有Razor / C#代码,只有HTML才是结果。

What you ask is like asking what type of oven was used to bake a pizza from the pizza. 您所要问的就像是询问使用哪种类型的烤箱从比萨烤制比萨。 Bad news - you never will know. 坏消息-您永远不会知道。

If you provie sensible tags from those, you could parse them in javascript, but you have to output that metadata yourself as part of the generated html. 如果您从中提供明智的标签,则可以在javascript中对其进行解析,但是您必须自己输出该元数据作为生成的html的一部分。

After reading your comment to TomTom; 阅读您对TomTom的评论后; the answer is: 答案是:

No. Razor does not come with any public syntax parser. 不能。Razor不附带任何公共语法解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM