Search code examples
asp.netasp.net-mvcajaxcontroltoolkitwebforms

Converting to Mvc, ToolkitScriptManager ServiceReference


So, I'm working in a hybrid web app/mvc application. Trying to do all new stuff in MVC and slowly changing some old webforms parts when there is time or a need for it. Using Razor.

Currently I'm trying to bring some old javascript into an mvc page and I'm having some troubles with the ServiceReferences. Is there a way to get these working in Razor, I really don't want to have to re-write this js file, seems how it's about 1500 lines.

I've got references to

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js">   </script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/mvc/2.0/MicrosoftMvcAjax.js"></script>

I'm guessing there must be some way to write this registration somewhere? In webForms it worked like this, for reference:

<ajaxToolkit:ToolkitScriptManager ID="cnvScriptManager" EnablePartialRendering="true" ScriptMode="Release" LoadScriptsBeforeUI="true" CombineScripts="true" CombineScriptsHandlerUrl="~/CombineScriptsHandler.ashx" runat="server">
                <Services>
                    <asp:ServiceReference Path="~/ux/controls/util.asmx" />
                    <asp:ServiceReference Path="~/ux/controls/menu/menu.asmx" />
                    <asp:ServiceReference Path="~/ux/controls/menu/list.asmx" />
                    <asp:ServiceReference Path="~/ux/controls/menu/tag.asmx" />
                    <asp:ServiceReference Path="~/ux/controls/canvas/filter/filter.asmx" />
                    <asp:ServiceReference Path="~/ux/controls/canvas/export/export.asmx" />
                </Services>
            </ajaxToolkit:ToolkitScriptManager>

and then in the js you could do this nonsensical magic:

Xyz.Api.ListUXService.Fill(listParams, onFillListSuccess, onFailure, List);

I would love to have the time to convert it to jQuery and MVC, but, I don't :)


Solution

  • in your razor view add a reference to a partial view, this partial view should be made with the webforms view engine

    <body>
        @Html.Partial("_ScriptManager")
    </body>
    

    and then just put your scriptmanager in said partial view

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
    <form id="form1" runat="server">
    <ajaxtoolkit:toolkitscriptmanager id="myScriptManager" enablepartialrendering="true"
        scriptmode="Release" loadscriptsbeforeui="true" combinescripts="true" combinescriptshandlerurl="~/CombineScriptsHandler.ashx"
        runat="server">
                    <Services>
                        <asp:ServiceReference Path="~/ux/controls/util.asmx" />
                        <asp:ServiceReference Path="~/ux/controls/menu/list.asmx" />
                        <asp:ServiceReference Path="~/ux/controls/menu/tag.asmx" />
                        <asp:ServiceReference Path="~/ux/controls/canvas/dialogs.asmx" />
                    </Services>
    </ajaxtoolkit:toolkitscriptmanager>
    </form>
    

    Now before all the mvc purists get mad, I want to add that I do not recommend doing this, this goes against MVC, but it works and if you don't have time to change a ton of legacy code, here is a solution.