PDA

View Full Version : [CLOSED] [#505] Mixing MVC and non-MVC Ext v2x



betamax
Jun 03, 2014, 5:04 PM
Is it possible, and what is correct way to use Ext.net MVC version in mixed environment (web forms and MVC)?

Can we install both versions from Nuget library and us them in the mixed environment?

We are encountering problems (rendering issues) when moving from an Ext MVC page back to an Ext webform page because of the following code:


public static void MarkAsMVC()
{
if (HttpContext.Current.Application["Ext.Net.MVC.IsMVC"] == null)
{
HttpContext.Current.Application["Ext.Net.MVC.IsMVC"] = new object();
}
}

public static bool IsMVC
{
get
{
return HttpContext.Current.Application["Ext.Net.MVC.IsMVC"] != null;
}
}

Can this method and property below be changed to use Items collection instead of Application state, or is there a simple solution?

We are currently using v2.4 and will not be able to upgrade until after our next release in late July.

Vladimir
Jun 03, 2014, 6:45 PM
Hi

What kind of problem do you have? Should not be any problems to use webform page inside MVC application

betamax
Jun 03, 2014, 8:24 PM
Simple explanation of problem is that direct methods are not registered on client.
When we attempt to call a direct method, in this case, to reload the page in "Edit" mode, the error in javascript is "Ext.net.DirectMethods is undefined".

.. and I think it is because of this:


public virtual string BuildDirectMethodProxies(bool dynamicOnly)
{
if (this.DirectMethodProxy == ClientProxy.Ignore)
{
return "";
}
#if MVC
if (this.IsMVC)
{
if (dynamicOnly || this.ViewContext == null)
{
return "";
}

return Ext.Net.MVC.MvcDirectMethod.BuildProxy(this.ViewCo ntext);
}
#endif


I would expect that ViewContext is obviously null for Web Forms page.
Even if it is not, code would go to Ext.Net.MVC.MvcDirectMethod.BuildProxy

Vladimir
Jun 03, 2014, 8:29 PM
Direct methods cannot be registered inside WebForm page in the MVC application because WebForm page is requested through controller any way (you return ViewResult to render that page). Therefore direct method handler is not available after webform page rendering

betamax
Jun 03, 2014, 9:04 PM
I am not sure I follow your logic. We use directmethods on 85% of the pages in our application. We have been using Ext since v.8. The problem only occurs when we go from an MVC page to an Ext v2.4 page. Once we do that, the flag , isMVC, is set, and we no longer have access to DirectMethods functionality.

Vladimir
Jun 03, 2014, 10:12 PM
Do not confuse MVC application and WebForm application
You can use WebForm page inside MVC aplication but the page instance is created only once (when you render it inside controller action using ViewResult) instead every request page recreation in WebForm application

So, the page instance is never recreated after webform page rendering in the MVC application and we cannot get direct event/method handler after that. All direct event/method handlers must be controller actions only in MVC application (even if request was initiated from webform page)



I am not sure I follow your logic. We use directmethods on 85% of the pages in our application. We have been using Ext since v.8. The problem only occurs when we go from an MVC page to an Ext v2.4 page. Once we do that, the flag , isMVC, is set, and we no longer have access to DirectMethods functionality.

You use it in WebForm application (not MVC application). WebForm and MVC applications have different life cycle. Do not rely WebForm concepts inside MVC application. You cannot reach direct handler in web form page (view) even if you remove that flag because page (view) instance is not recreated like in WebForm application

cihoric
Jun 04, 2014, 7:52 PM
This might clarify situation that we have (or make it more confusing).

Timeline:
- WebForms application was using Ext.Net library.

- WebForms application was modified to support MVC architecture. (we got hybrid that is serving both MVC views and WebForms aspx pages)

- In order to use Ext.Net controls in razor views Ext.Net was replaced with Ext.Net.MVC.

- Also expectation was that Controls from Ext.Net.MVC version should work in WebForms environment and that seems to be truth.

- WebForms (aspx) pages with Ext.Net controls continued to work correctly (although we now are using Ext.net.MVC)

- When debugging from WebForms pages ResourceManager.IsMVC is never true.

- Ext.net ResourceManager is added to views and from the moment that view is hit WebForms pages are not working as expected.

Reason - ResourceManager.IsMVC returns true for every request (even for those that are handled by WebForms)

Reason - HttpContext.Current.Application["Ext.Net.MVC.IsMVC"] is set when resource manager helper is used on view.

Could this be possible solution - Use HttpContext.Current.Items["Ext.Net.MVC.IsMVC"]

This would make ResourceManager.IsMVC property "request specific" and prevent using MVC specific code in BaseControl when controls are rendered for WebForms request.

I hope that this make some sense


Thanks
Milan

Vladimir
Jun 04, 2014, 8:14 PM
Well, you have to understand that WebForm page life cycle is different in the MVC application if compare with WebForm application
Any handlers inside WebForm page cannot be executed after that page rendering because MVC application doesn't recreate page instance like WebForm application when you request a page



Reason - ResourceManager.IsMVC returns true for every request (even for those that are handled by WebForms)

yes, ResourceManager.IsMVC is application level property (not per page/view)



Reason - HttpContext.Current.Application["Ext.Net.MVC.IsMVC"] is set when resource manager helper is used on view.


No, the reason is described in top of this post (page is not recreated automatically for requests)



Could this be possible solution - Use HttpContext.Current.Items["Ext.Net.MVC.IsMVC"]

This would make ResourceManager.IsMVC property "request specific" and prevent using MVC specific code in BaseControl when controls are rendered for WebForms request.


No, replacing Application to Items will not solve your issue. Just take note of direct event/method handlers must be defined in controller class (even if request was initiated from WebForm page) because it is MVC application, all requests are handled by controller only (or before controller), request cannot be handled by view (webform page)

cihoric
Jun 04, 2014, 10:21 PM
As I said we have hybrid setup
WebForms aspx pages are not processed by mvc engine (we are not using them instead of razor views if that is what you think), they have code behind and they are going through full life cycle.
Pages are working good, DirectMethods are registered on client, everything is working fine as if we are not using Ext.Net.MVC at all.
Problem appears when resource builder is used on one of the views that is actually processed by MVC engine.
After that we still have full page life cycle on those pages but resource manager is considering itself as MVC and starts producing results for MVC app. (meaning no more DirectMethods scripts - which I understand are not good for MVC anyway)
But thing is we are using also pages with code behind that are can work with direct methods.

Reason why our pages are working ok is that MarkAsMVC() is called only when you use MvcResourceManagerBuilder (called from ResourceManager helper).
And as long IsMVC is false we are ok

public virtual string BuildDirectMethodProxies(bool dynamicOnly)
{
if (this.DirectMethodProxy == ClientProxy.Ignore)
{
return "";
}
#if MVC
if (this.IsMVC)
{
if (dynamicOnly || this.ViewContext == null)
{
return "";
}

return Ext.Net.MVC.MvcDirectMethod.BuildProxy(this.ViewCo ntext);
}
#endif
Dictionary<string, Dictionary<string, DirectMethodList>> methods = this.GroupDirectMethodsByNamespace(dynamicOnly);
StringBuilder sb = new StringBuilder(256);

foreach (KeyValuePair<string, Dictionary<string, DirectMethodList>> ns in methods)
}
So your code will produce DirectMethods

When resource builder is created it is calling MarkAsMVC() and setting that application property and IsMVC becomes true.

Anyway I had inherited MvcResourceManagerBuilder just to do this

public class VMSResourceManagerBuilder : MvcResourceManagerBuilder
{
#region Constructors and Destructors

/// <summary>
/// Initializes a new instance of the <see cref="VMSResourceManagerBuilder"/> class.
/// </summary>
public VMSResourceManagerBuilder()
{
HttpContext.Current.Application["Ext.Net.MVC.IsMVC"] = null;
}

#endregion
}



public static VMSResourceManagerBuilder VMSResourceManager(this BuilderFactory factory)
{
return new VMSResourceManagerBuilder();
}
And this is helping us.

I understand that we are probably misusing Ext.Net.MVC inside Webforms pages but it seems that everything else is ok because in your code it is always checking for that IsMVC flag, so if it is false code will produce result as it would Ext.net.

Fell free to quit this thread if you think that this is not leading anywhere.

Thanks for your time and help so far

Milan

Vladimir
Jun 05, 2014, 1:11 PM
I am not familiar with such hybrid system which you use.
Can you provide more details (for example, articles in internet)?

Jsut intresting, at which place, web form page is recreated automatically in MVC application

cihoric
Jun 05, 2014, 2:12 PM
Hi Vladimir.

First thank you for bearing with us :)

Before going more into this could you please answer this question
Do you know if there are any side effects in using Ext.Net.MVC nuget package inside WebForms application?
Like if I do something like this:
New Project => choose Web Application => choose Web Forms template => and install nuget packge Ext.NET.MVC
Will this work?

Now to your questions.
This what our coworker followed to achieve this functionality:
http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4We bFormsApplications.aspx

And here are break points in our code during request to aspx page, mvc view and back to aspx page.
11911

Thanks once more for your time

Milan

Vladimir
Jun 05, 2014, 2:42 PM
Before going more into this could you please answer this question
Do you know if there are any side effects in using Ext.Net.MVC nuget package inside WebForms application?
Like if I do something like this:
New Project => choose Web Application => choose Web Forms template => and install nuget packge Ext.NET.MVC
Will this work?


Yes, it will work correctly. Our Examples Explorer works on Ext.Net.MVC without any problems

Thanks for the link, i will investigate it. Do you have a template or simple example of such application for testing?

cihoric
Jun 05, 2014, 4:34 PM
Hi Vladimir

It is great that Ext.net.MVC fully supports WebForms makes me trust more in our workaround.

Sorry but we don't have simple template.
Maybe you can try with this code although I am not sure if it is same approach as we had based on Hanselman's blog.
http://www.codeproject.com/Articles/38778/ASP-NET-WebForms-and-ASP-NET-MVC-in-Harmony

BTW if you are going to test maybe you can try with using Items instead of Application in MarkAsMVC

HttpContext.Current.Application["Ext.Net.MVC.IsMVC"] = new Object();

Thanks
Milan

Daniil
Jun 09, 2014, 12:22 PM
Created an Issue.
https://github.com/extnet/Ext.NET/issues/505

michaeld
Jun 09, 2014, 11:25 PM
I don't know if this is the same topic but I've wondered about combination of page models myself. In VS 2013, you have the ability to install both webform and mvc into the same virtual host more seamlessly. My guess would be that an application level setting for IsMVC would probably break. I haven't tried but I thought I'd bring up the VS13 scenario.

http://www.youtube.com/watch?v=tqK6FD45HWM

cihoric
Jun 10, 2014, 5:58 PM
I don't know if this is the same topic but I've wondered about combination of page models myself. In VS 2013, you have the ability to install both webform and mvc into the same virtual host more seamlessly. My guess would be that an application level setting for IsMVC would probably break. I haven't tried but I thought I'd bring up the VS13 scenario.


Good point.
I was trying to explain with our project but I think that new project in VS2013 with WebForms and Mvc selected would have same issue.

Daniil
Jun 11, 2014, 5:50 AM
Just to confirm the problem.

I've just created a hybrid WEbForms/MVC app in VS 2013.

DirectMethods are not registered on a WebForm page anymore after launching an MVC view with

@Html.X().ResourceManager()

We should do something with that.

cihoric
Jul 02, 2014, 8:59 PM
Is there any progress on this.
We would prefer if we don't need to use the tweak that I have implemented.

Daniil
Jul 03, 2014, 9:10 AM
Unfortunately, no progress yet. We'll let you know if something appears.

anup
Aug 27, 2014, 2:21 PM
I too would be interested in a solution to this as we are recently hitting the same problem.

An additional workaround that we have settled on, btw, is to by-pass the DirectMethod and use an ASHX or move the original code in the DirectMethod into a Controller action and invoke using something like:



Ext.net.DirectMethod.request({
url: /youraspx/or/mvccontroller
cleanRequest: true,
success: anySuccessHandlerHere,
failure: anyFailureHandlerHere,
complete: anyCompleteHandlerHere
});


(More info in #6. Disable the DirectMethod ClientProxy Creation section of this URL: http://examples2.ext.net/#/Events/DirectMethods/Overview/ )

For us, this workaround is good because we actually want to move mostly to MVC Controllers and ASHX and eventually minimize WebForms usage (though it is nice for very quick testing, prototyping and even simple pages where some integrators of our products are more familiar and comfortable with WebForms).

Hope that helps.

Daniil
Aug 28, 2014, 9:20 AM
Hi Anup,

Thank you for your valuable input.

Daniil
Sep 19, 2014, 1:01 PM
We have changed "Application" to "items" for the IsMVC marker. It has been done in the revision #5978 (SVN trunk). It should help with your scenario.

Please update and retest. We would appreciate if you confirm it works for you or not.

anup
Dec 03, 2014, 2:36 PM
Hi,

I was going through old threads and realized I forgot to reply to this one. Apologies for that. Yeah, it seems to be working for me. Many thanks!

Anup

cihoric
Dec 03, 2014, 2:56 PM
I just received notification when anup posted his reply. (had no notifications for long time)

Anyway I think that we are upgrading to latest version of EXT in few days/weeks but I think that solution will work for us as I tested that before starting this thread.

Thanks

Daniil
Dec 03, 2014, 3:16 PM
Thank you for the updates!