locked
cshtml (htmlx) FileCodeModel RRS feed

  • Question

  • Hi!

    Is it possible to access code model for htmlx (.cshtml) files?

    I tried to accessing FileCodeModel for .cshtml files using code below but with no success.

    _dte.ActiveWindow.Document.ProjectItem.FileCodeModel

    It works for C# and C++ file sources but it returns null for htmlx.

    Thanks,

    Jake

    Monday, September 1, 2014 5:26 PM

Answers

  • After a long and painful search I managed to find this:

    using System.Web.Razor;
    using System.Web.Razor.Parser.SyntaxTree;

    var host = newRazorEngineHost(newCSharpRazorCodeLanguage()); var engine = newRazorTemplateEngine(host); var result = engine.ParseTemplate(newStringReader(<STRING>)));

    result.Document.Children provides detailed recursive tree structure.

    • Marked as answer by Jaka Konda Friday, September 5, 2014 5:31 PM
    Friday, September 5, 2014 5:31 PM

All replies

  • Hi Jake,

    I would like you to check these documents firstly:

    Discovering Code by Using the Code Model (Visual C#)

    CodeModel Interface

    CodeElements Interface

    If it returns null, it means that it is not a code file. According to the documents above, you could find that the code model in Visual Studio doesn't proivde support for all the languages, even languages like C# or VB.NET do not implement the entire Visual Studio code model, because some elements in the code model are language-specific.

    On the other hand, cshtml is not a language, but more like a file template which contains C# and html language, along with some special marks. It's not a standard C# or C++ code file. So I don't think it's posibble to access "cshtml code model". You need to analyze the file by yourself.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Jaka Konda Wednesday, September 3, 2014 7:44 AM
    • Unmarked as answer by Jaka Konda Friday, September 5, 2014 5:31 PM
    Wednesday, September 3, 2014 7:12 AM
  • After a long and painful search I managed to find this:

    using System.Web.Razor;
    using System.Web.Razor.Parser.SyntaxTree;

    var host = newRazorEngineHost(newCSharpRazorCodeLanguage()); var engine = newRazorTemplateEngine(host); var result = engine.ParseTemplate(newStringReader(<STRING>)));

    result.Document.Children provides detailed recursive tree structure.

    • Marked as answer by Jaka Konda Friday, September 5, 2014 5:31 PM
    Friday, September 5, 2014 5:31 PM