Skip to content

Feature request: index Razor (.cshtml) files — embedded C# regions + script blocks #319

@Johnny-Tsai

Description

@Johnny-Tsai

Summary

Please add support for indexing Razor (.cshtml) files in CodeGraph's
tree-sitter pipeline — at minimum the embedded C# regions and inline <script>
blocks. Today .cshtml files are skipped entirely, which leaves a large blind
spot for ASP.NET MVC / Razor Pages codebases.

This is conceptually the same request as #309 (Astro .astro frontmatter +
script blocks): a hybrid template format where significant logic lives in the
file but is currently invisible to the graph.

Motivation

ASP.NET MVC (the classic .NET Framework / ASP.NET Core MVC stack) stores its
entire view layer in .cshtml files. In a real-world MVC project these files
hold:

  • The view ↔ ViewModel binding (@model SomeViewModel)
  • References to controller actions (@Url.Action(...), @Html.ActionLink(...))
  • C# code blocks and helpers (@{ ... }, @functions { ... }, @helper ...)
  • Inline expressions referencing model members (@Model.SomeProperty)
  • Embedded JavaScript in <script> blocks

After indexing a large MVC solution, the language breakdown showed csharp,
javascript, typescript, etc. — but zero Razor, because .cshtml isn't in
the extension→language map. For MVC projects this is the single biggest coverage
gap: an AI agent gets full backend C# context but cannot see which view binds to
which ViewModel, or which controller action a view calls.

What a Razor file looks like

@model MyApp.ViewModels.OrderViewModel    @* viewViewModel binding *@

@{
    var total = Model.Items.Sum(i => i.Amount);   // C# code block
}

<div>@Model.CustomerName</div>                     @* inline C# expression *@
<a href="@Url.Action("Edit", "Order")">Edit</a>   @* controller action ref *@

@functions {
    string Format(decimal d) => d.ToString("N0");  // local C# helpers
}

<script>
    function save() { /* ... */ }                  // embedded JS
</script>

Suggested scope (pragmatic, not a full HTML parse)

To keep this tractable (mirroring how #309 frames Astro), it would be valuable
even if CodeGraph only extracts the embedded code regions rather than fully
parsing the HTML:

1. @model <Type> → a reference edge from the view file to the ViewModel type.
2. @{ ... }, @functions { ... }, @helper ... → parse the contents with the
existing C# tree-sitter pass (symbols + references).
3. @(...) / @Model.X / @Url.Action(...) / @Html.* → reference extraction
(which symbols / controller actions a view touches).
4. <script> ... </script> → parse with the existing JavaScript pass.

Even partial coverage (just #1 and #2) would let agents answer "which view uses
this ViewModel?" and "what does this view depend on?" — currently impossible.

Notes / open questions

- There are community tree-sitter-razor grammars, but they're immature. A
lighter approach (region extraction → reuse the existing C#/JS passes) may be
more robust than depending on a full Razor grammar.
- Same pattern applies to other server-side template families (e.g. .vbhtml),
though C# Razor is by far the most common.
- Happy to provide more real-world .cshtml samples / edge cases if useful.

Impact

ASP.NET MVC + Razor is a very large ecosystem; this would meaningfully extend
CodeGraph's usefulness for any .NET web codebase, which currently gets

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions