MVCSiteMapProvider with Webforms

423 Views Asked by At

I have inherited a hybrid MVC / Webforms project, the MVC portion uses MVCSiteMapProvider to create the menus and the WebForm portion uses a custom built menu creation tool.

I have just created some custom MVCSiteMapProvider nodes and would like to port the whole MVCSiteMapProvider to be used in the webform portion of the project. Is this possible at all? Can anyone suggest any strategies for wedging/hacking it in to the webform code?

thanks Andy

2

There are 2 best solutions below

4
On

Although in theory we should have working interoperability with WebForms, to my knowledge it hasn't been tried or even tested. But then, if there were no issues with setting it up, I probably wouldn't have heard about it.

To set it up:

  • Add a single XML file that is shared between ASP.NET and MvcSiteMapProvider and configure all of your nodes there. Make sure to use the namespace declaration from the Mvc.sitemap file that is installed by the NuGet package. Note that you can safely add the MvcSiteMapProvider attributes and they will just be treated like custom attributes by XmlSiteMapProvider (and essentially ignored). To make it possible to navigate between WebForms and MVC, you should use the url attribute instead of configuring controller, action, etc.
  • Webforms would use the XmlSiteMapProvider and all of the controls that utilize it to display the Menu and SiteMapPath. Basically, set this up like you would a normal ASP.NET website using the MSDN documentation as a guide.
  • MVC would use MvcSiteMapProvider and its HTML helpers to display similar functionality. Note that you can customize the HTML helpers or build your own if that is what you need to meet your requirements.
  • For security trimming, you can use the xmlRolesAttribute for WebForms, and the AuthorizeAttribute in MVC.
  • For localization, you can pretty much just follow the MSDN documentation.

That is basically all of the functionality that the default ASP.NET sitemap providers have, so you wouldn't have access to visibility providers, dynamic node providers, or some of the other more advanced features of MvcSiteMapProvider when working within WebForms unless you build your own System.Web.SiteMapProvider that can handle those details.

Since none of the HTML helpers post back, you might also be able to get away with just writing the output of the Menu and SiteMapPath HTML helpers in the WebForm, although I am skeptical this will work because the HTML helpers are templated, which probably means they only work in MVC views. Building your own HTML helpers that output HTML or even output ASP.NET controls is also potentially possible because they are simply extension methods that can be called from WebForms.

Please provide any feedback by opening a new issue @ GitHub.

0
On

You can use two different sitemap files. One web.sitemap with XMLSiteMapProvider for the webforms and another for MVC Portion Mvc.Sitemap with MVCSiteMapProvider. For the webforms specify the url instead of action and controller in the Mvc.SiteMap. I am currently using this in our hybrid project and it works well.