Click here to Skip to main content
13,357,610 members (35,866 online)
Rate this:
Please Sign up or sign in to vote.
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>

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

<div id="content1">

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

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?
Posted 8-Dec-12 8:29am
Updated 9-Dec-12 19:55pm
Zoltán Zörgő 8-Dec-12 13:54pm
Please clarify: you want to "run" cshtml file with razor view syntax outside MVC3 (or later), in a windows forms or console application for example?
askquez 8-Dec-12 14:40pm
I want to use cshtml file in mvc3 . I already created project in mvc3 now i want to change it html element contents of cshtml file by c#. I am building simple template builder that extract html from any html template and save this html in my csthml file that i already created.
askquez 8-Dec-12 14:18pm
i want to extract inner html from any html file where element id will be "content1" and place this inner html in cshtml file where element id is "content1"
Zaf Khan 9-Dec-12 5:10am
My Apologies to you
Zoltán Zörgő 9-Dec-12 12:18pm
Sorry, I can't get your intentions. Please give a more detailed usage example of what you want.
askquez 10-Dec-12 0:54am
Ok i am going to edit my question simply i want razor file parser that can parse both html and razor syntax like html parser
Zoltán Zörgő 10-Dec-12 12:36pm
Razor parser is html parser, since razor parser handles all html as string constant. Razor is a templating engine. How do you want to "parse" the html? What html exactly? You type the html, the razor perser is only handling the code. It has absolutely no knowledge about the DOM where the code resides. It can not know anything about it, since the final output can be compound from many cshtml executions.

If you want to embed a simple html in a cshtml file using a helper, than you can use Html.RenderPartial for example. The rendered file has to be cshtml (or aspx), and has to reside among the other views, but it is not needed to contain any code at all. But it is easy to write a Html.RenderContent helper too: just read the content and output as HtmlString. That's all.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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 [[^]

hope it helps
askquez 11-Dec-12 0:42am
@adama74 i also think on this solution how can i use regular expression in HtmlAgilityPack.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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>));


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 2 Jun 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100