如何解析asp.net mvc razor视图(cshtml)就像c#中的html解析器 [英] how parse asp.net mvc razor view (cshtml) like html parser in c#

查看:458
本文介绍了如何解析asp.net mvc razor视图(cshtml)就像c#中的html解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#中解析剃刀视图文件。我也使用Html Agility Pack来解析剃刀视图文件但是它无法保存正确的文件内容。



基本上我想通过服务器端更改内部html的一些html元素使用c#



I want to parse razor view file in c# . I have also used Html Agility Pack to parse razor view file but it failed to save correct file contents.

Basically i want to change some html elements inner html by server side using c#

<div id="content1">
<p>this contents i want to change </p>
<span>contes</span>
</div>



i希望通过c#更改content1内部html像这样




i want to change content1 inner html by c# like this

<div id="content1">
    <span>@Function.gethtml()</span>
    </div>





我使用了html agility pack来改变内部html内容,但它没有解析razor语法功能,比如





I have used html agility pack to change inner html contents but it is not parsing razor syntax function like

@Url.Content("abc.css") and other function





我想要razor文件解析器,它可以解析html解析器之类的html和razor语法



有没有办法读取文本文件并替换目标div内容或div本身



任何解决方案?



i want razor file parser that can parse both html and razor syntax like html parser

Is there any way to read as text file and replace target div contents or div itself

Any solution?

推荐答案

我也正在做类似的任务 - 我的是提取测试 - 来自html / cshtml / aspx / etc的节点。类型文件。

到目前为止,我使用HtmlAgilityPack来解析HTML - 但是如何在解析过程中区分Razor标记和纯文本 - 这就是问题。



我还没有尝试过,但是一个(可能的)解决方案是使用正则表达式匹配剃刀类型语法;见[ http://stackoverflow.com/questions/7767026/regular-expression- to-match-razor-like-expressions [ ^ ]



希望它有所帮助
I'm also working on a similar task - mine is to extract test-nodes from html/cshtml/aspx/etc. type files.
So far, I am using the HtmlAgilityPack to parse the HTML - but how to distinguish Razor markup from plain-text during parsing - THAT is the question.

I haven't tried it out yet, but one (possible) solution would be to use regular-expressions to match razor-type syntax; see [http://stackoverflow.com/questions/7767026/regular-expression-to-match-razor-like-expressions[^]

hope it helps


我认为你正试图使用错误的工具...



解析剃刀代码修改它是没有意义的。使用母版页,html帮助程序或您可能扫描的序列。或者更好的是,为什么不使用剃刀本身来生成代码?



您只需在模型中添加可在特定位置输出的内容并在您的razor代码,如果你有输出的东西。
I think that you are trying to use the wrong tool for the job...

It make no sense to parse razor code to modify it. Use master pages, html helpers or maybe your own sequence that you would scan. Or better, why not uses razor itself to generate your code?

You simple need to add in your model the content that sould be outputted at specific locations and test in your razor code if you have something to output.


How about you use the actual Razor template engine to do the job for you.

Make sure you have

<pre lang="c#">using System.Web.Razor;





然后是类似的东西





and then something like that

RazorEngineHost _host = new RazorEngineHost(new CSharpRazorCodeLanguage());




RazorTemplateEngine templateEngine = new RazorTemplateEngine(_host);




var parserResult = templateEngine.ParseTemplate(new StringReader(<YOU CONTENT HERE>));





干杯,

Andrzej



Cheers,
Andrzej


这篇关于如何解析asp.net mvc razor视图(cshtml)就像c#中的html解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆