Running Ajax.NET Professional (AjaxPro) under ASP .NET MVC(转)

AjaxPro.dll 在MVC中应用,找了好久找到了解决办法,拷来的,原文地址如下:

http://my6solutions.com/post/2009/03/09/Running-AjaxNET-Professional-under-ASP-NET-MVC.aspx

Note: With ASP .NET MVC, jQuery is the more convenient method you could use to make Ajax calls.

Ajax.NET is the first Ajax library for the .NET framework. Up until the day ASP .NET 3.5 was released, there was nothing quite like it. In ASP .NET 2.0, the UpdatePanel was introduced as a way to enable partial rendering on ASP .NET pages. UpdatePanels are quick to implement. However, the downside of it is its inefficiency. The viewstate of the page gets posted back as well! This gives you a bloated asynchronous request and basically defeats the purpose.

It is during times like this that Ajax .NET looks very attractive. With support for PageMethods in ASP .NET 2.0 (in Ajax.NET the attribute tag [AjaxMethod] is used instead), JSON serializers, built-in caching, javascript compression, synchronous calls, etc. what is there not to like about Ajax .NET. You could even return DataTables, DataSets, enums, and other custom structures to the client-side directly.

It is also an open source project and the code is available here. Unfortunately, development of Ajax.NET has "stopped", and the creator himself has suggested the use of the Ajax features provided by ASP .NET 3.5. However, the project itself is still being updated once in awhile. I guess it's hard to completely let go of a project that you've been working on for a long time. People just get attached to their code. Well... I know I do.

Anyway, I have been using Ajax.NET since .NET 2.0, as well as since .NET 3.5. I haven't found a good reason to switch and it even runs under ASP .NET MVC and I reckon it is still better than using Ajax.ActionLink(). Unless of course, there's some easier method that I do not know of then please feel free to enlighten me anytime. Most of my Ajax use centers around requesting data from the server asynchronously and then using javascript to update the DOM.

Installing Ajax.NET

To include Ajax.NET into your MVC project, follow the following steps:

  1. Go http://ajaxpro.codeplex.com and download the latest release, i.e., 9.2.17.1 
  2. Extract AjaxPro.2.dll somewhere safe so that we can add a reference to it late in our MVC project. The other files are for the JSON serializer and for use under ASP .NET 1.1. The web.config in the zip file gives you a bunch of configurable options.
  3. Then from VS, add a reference to it in your project. 
  4. In your web.config, add the following Under <configSections>

     

       12 <sectionGroup name="ajaxNet">

       13             <section name="ajaxSettings" type="AjaxPro.AjaxSettingsSectionHandler,AjaxPro.2" requirePermission="false" restartOnExternalChanges="true"/>

       14         </sectionGroup>


    Under <configuration>

     

       30 <ajaxNet>

       31         <ajaxSettings>

       32             <token enabled="true" sitePassword="notmypassword" />

       33         </ajaxSettings>

       34     </ajaxNet>

    Under <httpHandlers>

     

      107 <add verb="*" path="/ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>

     

  5. In global.asax, you will then need to add the following line to your RegisterRoutes() method to allow AjaxPro to handle requests directed at it.

   30 routes.IgnoreRoute("ajaxpro/{*pathInfo}");


Using Ajax.NET

To be able to call your class methods on the client-side, you will need to add the [AjaxPro.AjaxMethod] attribute to the method. The AjaxMethod attribute can be used on any methods in any classes and is not limited to methods in the code-behind of your webpages. You will then need to inform AjaxPro parse the class containing your marked method to generate proxy classes for the client-side. This usually is done via the method

 

AjaxPro.Utility.RegisterTypeForAjax(Type t)

 

at the Page class. The other method is to directlypoint AjaxPro to the location by adding the script tag

 

<script type="text/javascript" src="/ajaxpro/className,assemblyName.ashx"></script>

 

The className MUST include it's namespace. Basically the above allows AjaxPro to find the method through reflection in the assembly to generate the appropriate proxy classes.

As there is not really a concept of pages in ASP .NET MVC, the second method is used and you will additionally need to add the following script tags (these are normally generated when you use RegisterTypeForAjax)

 

<script type="text/javascript" src="/ajaxpro/prototype.ashx"></script>

<script type="text/javascript" src="/ajaxpro/core.ashx"></script>

<script type="text/javascript" src="/ajaxpro/converter.ashx"></script>

And that should be all that needs to be done. If you want to know more about AjaxPro, you can visit its webpage at http://www.ajaxpro.info/. There is also an entry on how to use AjaxPro with jquery here.

原文地址:https://www.cnblogs.com/Guroer/p/1884514.html