Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.11/5 (2 votes)
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#

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

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

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


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

C#
@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
Updated 9-Dec-12 18:55pm
v3
Comments
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 asp.net mvc3 . I already created project in asp.net 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.

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
 
Share this answer
 
Comments
askquez 11-Dec-12 0:42am    
@adama74 i also think on this solution how can i use regular expression in HtmlAgilityPack.
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.
 
Share this answer
 
XML
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

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

C#
RazorTemplateEngine templateEngine = new RazorTemplateEngine(_host);

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


Cheers,
Andrzej
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900