ASP.NET MVC AutoEventWireup required?

1.6k Views Asked by At

when i create an aspx page, the header includes something like this:-

<%@ Page
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    AutoEventWireup="true" 
    CodeBehind="Create.aspx.cs" 
    Inherits="My.Mvc.Views.Blah" %>

With ASP.NET MVC apps, do we:

  • need to include this AutoEventWireUp attribute?
  • What happens if we set this to false?
  • what does this attribute really do? is it valid for ASP.NET MVC?

thanks heaps folks!

2

There are 2 best solutions below

0
On BEST ANSWER

Sorry - the default is true in ASP.NET, so you should explicitly set AutoEventWireup to false in the @ Page directive, or remove it and set it to false in pages section of web.config for MVC.

2
On

You can get rid of this attribute, or set it to false (which is the default).

AutoEventWireup means ASP.NET will use reflection at runtime to look for methods on your web form class in the form of Page_EventName (like Page_Load, Page_Init, etc), and automatically wire the methods to the corresponding page lifecycle events. I have some more details here: http://odetocode.com/Blogs/scott/archive/2006/02/16/2914.aspx

In MVC you should, as a general rule, avoid wiring up event handlers for the page lifecycle and code-behind in general.