How do I define a method in Razor?
Razor is just a templating engine.
You should create a regular class.
If you want to make a method inside of a Razor page, put them in an @functions block.
You could also do it with a Func like this
@{
var getStyle = new Func<int, int, string>((width, margin) => string.Format("width: {0}px; margin: {1}px;", width, margin));
}
<div style="@getStyle(50, 2)"></div>