Andreas Bonini
Andreas Bonini

Reputation: 44742

Convert razor .cshtml files to C# .cs files

I need to run some analysis on the source code of a web application, including its views, using Roslyn.

Since it can only parse C# files and not razor views, I planned to convert the views into uncompiled .cs files and then have Roslyn parse that.

How can I do that?

Upvotes: 5

Views: 4792

Answers (2)

opis-kladno
opis-kladno

Reputation: 127

I am generating .cs file from Razor .cshtml files with help of Razor Generator library.

Generated cs files contains line information from the .cshtml source files. So if you run your Roslyn analyzer against .cs files you can map the source line from .cs file back to the original .cshtml file.

Upvotes: 1

spender
spender

Reputation: 120380

I think the extension package Razor Generator is what you're looking for.

Generates source code from Razor files (.cshtml files), allowing them to be compiled into your assemblies. Supports MVC, Web Pages and standalone templates.

Upvotes: 11

Related Questions