Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC Razor Parser
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 want to change content1 inner html by c# like this
 
<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
 
@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 7:29am
askquez369
Edited 9-Dec-12 18:55pm
v3
Comments
Zoltán Zörgő at 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 at 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 at 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 at 9-Dec-12 5:10am
   
My Apologies to you
Zoltán Zörgő at 9-Dec-12 12:18pm
   
Sorry, I can't get your intentions. Please give a more detailed usage example of what you want.
askquez at 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ő at 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
good
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 [http://stackoverflow.com/questions/7767026/regular-expression-to-match-razor-like-expressions[^]
 
hope it helps
  Permalink  
Comments
askquez at 11-Dec-12 0:42am
   
@adama74 i also think on this solution how can i use regular expression in HtmlAgilityPack.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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>));
 
Cheers,
Andrzej
  Permalink  
v2

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



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