Shazwazza / ClientDependency

DEPRECATED. A library for managing CSS & JavaScript dependencies and optimization in ASP.Net
139 stars 65 forks source link

Html.RequiresJs in Umbraco macro throws 'A ViewUserControl can be used only in pages that derive from ViewPage or ViewPage<TModel>' #127

Closed praneethparupudi closed 7 years ago

praneethparupudi commented 7 years ago

Hi. I am trying to use a usercontrol as macro in Umbraco and when i try @Html.Partial("Footer") from your sample it works fine but when i try the same with @Umbraco.RenderMacro("Footer") it throws an error 'A ViewUserControl can be used only in pages that derive from ViewPage or ViewPage'. Does the ClientDependency not support the Umbraco macro's? It would be a shame if it does not work with macro's as we would like to use the built in Umbraco helpers.

Shazwazza commented 7 years ago

You are probably mixing two different technologies - Umbraco by default uses an MVC pipeline to render it's pages and templates and if you are using a "User Control" which is webforms you'll have problems with this since these are 2 very different technologies. Sure Umbraco does some magic to try to allow integration between the 2 but it is not possible for CDF to do that. You can try using Partial View Macros instead

I would recommend against using inline dependency registration into things like macros (which can also be cached ... which means you'll run into more problems since CDF declarations cannot be in a cached view).

praneethparupudi commented 7 years ago

Thank you so much for your reply. I should have been more clear. I also tried creating a partial view macro for my user control (which inherits from ViewUserControl). However this also resulted in the same error as internally it invokes @Umbraco.RenderMacro("Footer") in the partial view macro. When it did not work i tried invoking my user control via @Html.Partial("Footer") and it works.

We have some 80-90 user cntrols in our old application which do not have much logic in the code behind. Most of the logic is via ajax calls (controllers) and rendered in knockout. All we do is capture parameters in the user controls and pass it on to the controller via ajax calls. We are now migrating to Umbraco and the client does not want to migrate all the user controls to MVC.

In order to troubleshoot this i downloaded your footer.ascx from your test sample and if i invoke your footer via @Umbraco.RenderMacro("Footer") it does not work but if i use @Html.Partial("Footer") it works.

Bascially i created a Umbraco application in MVC rendering mode and used your footer.ascx as a partial view macro in a template page via @Umbraco.RenderMacro("Footer").

praneethparupudi commented 7 years ago

Sorry closed it by mistake :)

Shazwazza commented 7 years ago

we need to sync on terminology here since this is very confusing

So when you say:

I created a Umbraco application in MVC rendering mode and used your footer.ascx as a partial view macro in a template page via @Umbraco.RenderMacro("Footer").

What you mean is

I created a Umbraco application in MVC rendering mode and used your footer.ascx as a User Control Macro in a template page via @Umbraco.RenderMacro("Footer").

... which is exactly the problem I mentioned above - CDF will not work when mixing WebForms and MVC which is what you are doing. You can move your CDF declarations to your normal views instead of embedding them into macros, or you can create named bundles in CDF and render them as bundles (this is the preferred method). In reality registering assets as part of partial views, macros or components is quite an old way of doing things these days.

praneethparupudi commented 7 years ago

Yeah. I understand. Our scenario also uses bundles in the master page to load generic bundles which are common across the application but we also need few page specific javascript files when the user control macro is loaded. So i tried declaring them to the macro but now i will need to create wrapper views for these macro's and declare the scripts there. Thanks for your feedback.