I am trying to implement a MVC Razor _Layout.cshtml page that uses a WebForm ascx User Control (non-MVC). I am doing this based off the \"Yes\" section of this Scott Hansleman a
As the error message states, the user control "must derive from ... ViewUserControl
". In the code behind file for your user control, simply change this...
public partial class WebUserControl1 : UserControl
{
// ...
}
... to this:
public partial class WebUserControl1 : ViewUserControl
{
// ...
}
ViewUserControl
inherits from UserControl
, so it will continue to work in your existing WebForms pages.
You may have to deal with additional issues beyond this one in order to get your user control to work in MVC, though. At least one other that I encountered is (alluded to by SLaks):
Control 'controlId' of type 'controlType' must be placed inside a form tag with runat=server.
If you encounter stuff like this, you're going to have to get creative. Either modify the user control so that it can live happily in both WebForms and MVC (replace the offending controls with generic HTML equivalents - with all the implications of that), or duplicate it so you have a WebForms version and an MVC version.
For example, you will have to replace things like
with , which means you also need to modify the server-side code that handles the value coming from that input. Basically you have to neuter the user control, converting it from something that used to embody both view rendering logic and post-back handling logic, into something that just renders a view.
You can make one ascx
control play nicely with both WebForms and Razor MVC, and for simple things like page headers and footers or read-only views of data this is a good approach for migrating an application to MVC. But for more complex things like input forms, it will probably be easier to maintain both an ascx
user control and an MVC Razor view.