6

I have a website in asp.net 4.0 with 10 pages and 12 user controls.

Now i want to migrate this site in asp.net MVC 4.0; So can I re-use those user controls (that are there in traditional asp.net) and render them using <%= Html.RenderUserControl() %>.

In short my question is whether we can use asp.net's user control in asp.mvc 4?

Many thanks,

JIgar

Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
  • It depends. If they don't depend on ViewState, it might work. All comes down to what's in the control. – Craig Stuntz Mar 15 '12 at 13:53
  • Craig stuntz :- Can you lead me to some profound links? better answers? – Jigar Pandya Mar 15 '12 at 13:54
  • 1
    see this :http://stackoverflow.com/questions/6317317/mvc-3-add-usercontrol-to-razor-view – Jignesh Rajput Mar 15 '12 at 13:58
  • I'm not sure I know a better answer than, "Try it and see if it works." [I've made this work for DayPilot](http://blogs.teamb.com/craigstuntz/2009/05/12/38297/), but it fails for the SSRS viewer. – Craig Stuntz Mar 15 '12 at 13:59
  • @Jigs he doesn't seem to be using Razor. – Craig Stuntz Mar 15 '12 at 14:00
  • Look at this solution: http://malvinly.com/2011/02/28/using-web-forms-user-controls-in-an-asp-net-mvc-project/. Rxcept ViewState it's unuseful if usercontrol contains any actions (buttons/checbox etc). It wouldn't fire. – Denis Agarev Mar 15 '12 at 14:01

2 Answers2

5

You generally can't use asp.net controls in a MVC application. Almost all controls require view state, which ASP.NET MVC doesn't support.

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
  • You may be able to use some trickery to half have them work, but web forms controls require things like view state, and the page life cycle (init, load, ... prerender) and mvc is totally different. We encountered the same problem where we had a user control that we'd like to re-use in mvc and the short answer was essentially that we needed to rebuild it using razor views and models. Perhaps someone could build a web forms component to somehow include in a mvc page but I don't think there is such a thing at present. – Action Dan Feb 22 '17 at 20:02
0

Nice Question Jigar Pandya;

Yes; we can use ASP.NET WebForm User Controls in MVC Site.

One way we would be able to use user controls in ASP.NET MVC is to add a new item of type webform.

Then on that webform we would be able to use ASP.NET User Control's in a MVC project. I had the same question since we had many existing user controls we wanted to use. Hope that helps.

Catto
  • 6,259
  • 2
  • 52
  • 55
  • I can't see a way to add a "new item of type webform". I think you need to explain that part. The answer to the question seems to be that this is not possible. – Action Dan Feb 14 '17 at 19:48
  • In solution explorer right click "add new item" then under installed templates choose "web form" https://msdn.microsoft.com/en-us/library/0hds5k93.aspx Hope that helps you Action Dan – Catto Feb 15 '17 at 20:57
  • Hi that referenced page is titled "How to: Add ASP.NET Web Forms Page to a Web Project". The question is about adding a web forms *user control* to a mvc project. Something which apparently is not possible in the way some would hope. – Action Dan Feb 18 '17 at 10:23
  • When I read in your first comment you were trying to add a "new item of type webform" that is what I was linking about & in the answer I stated Yes; we can use ASP.NET WebForm User Controls in MVC Site. The UC can be on the webform page. Hopefully you can see how to add that as well. Hope that helps! – Catto Feb 21 '17 at 18:21
  • To clarify for other readers, although you can include a web form (aspx) file on your mvc site, and include user controls on that, you can not include your asp.net controls on a mvc razor view. They are not compatible. – Action Dan Feb 22 '17 at 20:01
  • 1
    Agree Action Dan; we can add a UC to an MVC project but we cannot add a UC to a MVC razor view. – Catto Feb 22 '17 at 22:23