1

With ASP.NET MVC 3, I am trying to migrate a webform page to a Razor page.

I could not find a way to migrate this kind of code I used this kind of code in webforms:

<script runat="server">
    protected override void OnInit(EventArgs e)
    {
          base.OnInit(e);
          my other code here...
     }
</script>

I could not find a way to translate this in Razor.

RPM1984
  • 72,246
  • 58
  • 225
  • 350
user385411
  • 1,445
  • 1
  • 18
  • 35

2 Answers2

2

Because ASP.NET MVC does not have the same "page lifecycle" like Web Forms does (that's a good thing). Have a look at this StackOverflow question to see the comparison of the two.

You can use _ViewStart.cshtml for this purpose (will execute before any View renders)

_ViewStart.cshtml:

@{
    @* Your code here, e.g: *@
    ViewBag.Theme = "MyTheme"
}
Community
  • 1
  • 1
RPM1984
  • 72,246
  • 58
  • 225
  • 350
0

How about an Action Filter. Action filters are custom attributes that provide a declarative means to add pre-action and post-action behavior to specific controller action methods. It sounds like that might be what you're looking for.

http://www.asp.net/mvc/mvc3

Bill Campbell
  • 2,413
  • 6
  • 27
  • 32