问题
I really like Spark, but we've already got a big investment in webforms based views. I'd like to start using Spark without having to convert all my existing views up front.
The only problem I have with getting Spark working side by side with webforms is the master pages. Currenly I have spark layouts that are 1 for 1 duplicates of the master pages.
This amount of duplication isn't acceptable. I've already been bitten by it several times.
Is there any way to get spark views to work within a webforms master? Or for a webforms view to work within a spark layout? Either would solve the duplication problem.
回答1:
The way I've accomplished this was to move most of the content of my master page into partial views, and then have two master pages: Foo.Master, and Foo.Master.spark that both use the same Html.RenderPartial() calls. Both masters have the same named content areas. This lets me use either engine for a given view and makes the duplication as small as possible.
An individual view engine is expected to handle both its master and view. The call to IViewEngine is:
FindView(System.Web.Mvc.ControllerContext controllerContext, string viewName, string masterName, bool useCache)
It does not render the master separate from the view.
Spark uses this to compile the master into the view and other tricks with its multi-pass rendering.
回答2:
I don't think so. Architecturally the two have completely different and conflicting ways of rendering the page. To make it happen you'll need to do something that is the equivalent of an iFrame, a terribly kludgey way of making it happen to the extent of being an anti-pattern.
By the time the master page is read, the spark engine will have already finished and vice-versa.
You could try to mimic how the master page is rendered within the spark engine. Not just visually ... I'm talking about cobbling together a home-rolled master page pre-processor using spark. Not an easy row to hoe.
回答3:
Just an idea, try to write a custom view engine (or tweak Spark's one) that will render the requested .spark view using Spark engine, and then call WebForms engine passing it the rendered content - I think this should be possible, but I have no knowledge about these internals. An easier way would be to save rendered .spark HTML into .aspx and have WebForms pick it - but the performance will not be acceptable.
Anyway, I'd say it should be possible but 1) will require complex tweaking of view engines and 2) you'll lose much of the Spark/layouts functionality since your .spark views will mostly render as without spark layouts at all.
Also, try to ask in Spark mailing list.
回答4:
Any luck on this?
I have been able to host a spark view in a System.Web.Mvc.ViewUserControl call Html.RenderView() from the spark view. I bet I could also host a Spark view within a System.Web.Mvc.ViewUserControl using Html.RenderView() as well. This introduces some options (all with overhead) of sharing the master page:
Write a simple wrapper .ascx for you .spark views. They'd had the same model object, the wrapper could call HtmlRenderPartial on the wrapped view.
(vice versa) Write a simple wrapper .spark for your .ascx controls.
When I tried have a view Index.spark use masterpage Site.Master, I received error message:
The view 'Index' or its master could not be found. The following locations were searched:
~/Views/LfgSettings/Index.aspx
~/Views/LfgSettings/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
Layouts\Site.spark
Shared\Site.spark
I don't know what these paths represent though, it looks like the search path for the Index view and the search paths for its masterpage. It seems though the .spark file cannot use a .master masterpage.
I wonder though if its possible to write a wrapper .master file that calls into a .spark file which has the correct content regions. Some Reflector'ing would probably dig up some interfaces that could be made to work together.
回答5:
You could use your existing master pages with a very simple .aspx page which simply calls Html.RenderPartial("MySparkView") to get you spark content inside your existing master pages.
来源:https://stackoverflow.com/questions/1537783/will-the-spark-view-engine-interoperate-with-webforms-master-pages