Skip to content

Instantly share code, notes, and snippets.

@carlin-q-scott
Last active June 9, 2020 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlin-q-scott/239174eb6197f06f2256af2286bd0a6e to your computer and use it in GitHub Desktop.
Save carlin-q-scott/239174eb6197f06f2256af2286bd0a6e to your computer and use it in GitHub Desktop.
Files for converting from mvc views to razor pages
These are regular expressions for finding and replacing ASP.NET HTML Helpers with tag-helpers.
This could probably be combined with the pagify.py script to run all of these automatically.
@Html.LabelFor\(model => model.([\w_]+), htmlAttributes: new { @class = ("[^"]+") }\)
<label asp-for="$1" class=$2></label>
@Html.LabelFor\(model => model.([\w_]+), "([^"]+)", htmlAttributes: new { @class = ("[^"]+") }\)
<label asp-for="$1" class=$3>$2</label>
@Html.EditorFor\(model => model.([\w_]+), new { htmlAttributes = new { @class = ("[^"]+") } }\)
<input asp-for="$1" class=$2 />
@Html.DropDownList\("([\w_]+)", ([^,]+), htmlAttributes: new { @class = ("[^"]+") }\)
<select asp-for="$1" asp-items="$2" class=$3></select>
@Html.DropDownList\("([\w_]+)", ([^,]+), "([^"]+)", htmlAttributes: new { @class = ("[^"]+") }\)
<select asp-for="$1" asp-items="$2" class=$4><option value="">$3</option></select>
@Html.TextAreaFor\(model => model.([\w_]+), (\d+), (\d+), new { @class = ("[^"]+") }\)
<textarea asp-for="$1" rows="$2" cols="$3" class=$4></textarea>
@Html.ValidationMessageFor\(model => model.([\w_]+), "", new { @class = ("[^"]+") }\)
<span asp-validation-for="$1" class=$2></span>
@Html.RouteLink\("([^"]+)", "([^"]+)"\)
@Html.ActionLink\("([^"]+)", "([^"]+)", "([^"]+)"\)
@Html.ActionLink\("([^"]+)", "([^"]+)", "([^"]+)", null, null\)
<a asp-page="/$3/$2">$1</a>
href="@Url.Action\("([^"]+)", "([^"]+)", new { ?area = ("[^"]+") }\)"
asp-page="/$2/$1" asp-area=$3
@Html.ActionLink\("([^"]+)", "([^"]+)", "([^"]+)", new { area = ("[^"]+") }, new { @class = ("[^"]+") }\)
<a asp-page="/$3/$2" asp-area=$4 class=$5>$1</a>
@Html.RouteLink\("([^"]+)", "([^"]+)", null, new { @class = ("[^"]+") }\)
<a asp-page="/$2" class=$3>$1</a>
@Html.ActionLink\("([\w\s]+)", "(\w+)", "(\w+)", null, new { @class = ("[\w-]+") }\)
<a asp-page="/$3/$2" class=$4>$1</a>
@Html.ActionLink\("([\w\s]+)", "(\w+)", "(\w+)", new { (\w+) = (\S+) }, null\)
<a asp-page="/$3/$2" asp-route-$4="@$5">$1</a>
# prepend all .cshtml files with @page
import glob
for filename in glob.iglob('**/*.cshtml', recursive=True):
if (filename.startswith('_') or filename.contains('/_')): continue
with open(filename, 'r', encoding="utf8") as original: data = original.read()
if (not data.startswith('\ufeff@page')):
with open(filename, 'w', encoding="utf8") as modified:
modified.seek(1)
modified.write("@page\n" + data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment