user385411
user385411

Reputation: 1465

migrating webforms to Razor

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.

Upvotes: 1

Views: 1254

Answers (2)

Bill Campbell
Bill Campbell

Reputation: 2473

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

Upvotes: 0

RPM1984
RPM1984

Reputation: 73132

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"
}

Upvotes: 2

Related Questions