简体   繁体   中英

Is there a project that auto-generates JavaScript proxy code to call ASP.NET MVC action methods?

Is there c# code that takes an existing controller, analyse its public Action methods and generate a JavaScript proxy class so that it can be easily called by other Javascript code? I already know we can use jquery to do a $.post and $.get to call our ajax services but I believe the process can be simplified by not having to specify the relative url of the AJAX web service URL and a parameter name for each parameter input.

For example, let's say we have the following C# controller:

public class CustomerController : Controller
    {

        public JsonResult Create(string name, string address)
        {
            return new JsonResult {Data = 11111};
        }

        public JsonResult Update(int id, string name, string address)
        {
            return new JsonResult {Data = true};
        }
    }

I would like to call the controller's AJAX action methods by using the following fashion.

Proxy.Customer.Create("Scott Gu", "Somewhere in Redmond").done(function(id) {
      /* id is an int and not an string */
      Proxy.Customer.Update(id, "Scott Gu", "Somewhere in Seattle");
});

Does a project exist that allow me to do this?

Update

it turns out there's no project that does what I asked for. Something that could be of use, besides SignalR, is Phil Haack's Controller Inspector project. It can inspect any given controller and reveal what action method it has, the parameters it accepts, their types, etc.

The following link contains the getter method for retrieving a details about a given controller. https://github.com/Haacked/CodeHaacks/blob/master/src/MvcHaack.ControllerInspector/ControllerDetailer.cs

Update 2

Doh. Phil Haack already developed a JavaScript proxy. Tutorial can be found here .

I know, it's an old question, but I just found a project which seems to match your requirements:

ProxyApi by Steve Greatrex
http://blog.greatrexpectations.com/2012/11/06/proxyapi-automatic-javascript-proxies-for-webapi-and-mvc/

This excellent another project allows you to do what you asked for.
http://jsnet.codeplex.com/
This poject auto-generates JavaScript proxies for MVC and WebApi controllers.

With this project, you will have also the Intellisense.

Example

window.test = function test() {
/// <summary>
///This example works.
///You have the Intellisense. It's great!!!
///No hard coded url.
///</summary>

//-- settings of ajax request.
var a = $dpUrlSet.Customer.Create.$action0.$AjaxSettings();

//-- your parameters of action method
a.data.name = "Scott Gu";
a.data.address = "Somewhere in Redmond";

//-- stringify
a.data = JSON.stringify(a.data);

//-- send ajax request
var xhr = $.ajax(a);

xhr.success(function (id) {
    /// <summary>Response of ajax request</summary>

    //-- settings of ajax request.
    var a = $dpUrlSet.Customer.Update.$action0.$AjaxSettings();

    //-- your parameters of action method
    a.data.id = id;
    a.data.name = "Scott Gu";
    a.data.address = "Somewhere in Seattle";

    //-- stringify
    a.data = JSON.stringify(a.data);

    //-- send ajax request
    var xhr = $.ajax(a);

});
}

I don't know of a project that does exactly what your after but have you looked at SignalR by David Fowl? This project has a javascript proxy generator based off a SignalR hub rather than an MVC controller. I'm sure the code can be modified though.

If you get the source code from github and look at the sample project there's a chat room sample. Using firebug/chrome dev tools etc you can see the javascript that is called.

From what I've looked at the javascript is created through a proxy. In the sample project there's a "hubs" folder that has methods that get inserted into a javascript file via the proxy.

The actual proxy stuff is done in the core SignalR library here , this is the default javascript proxy generator used.

You can see a live sample of the chat here , David Fowl is sometimes in the room as well. I'm sure he can explain the proxy stuff much better than myself.

Phil Haack has a project that solves 1 of my needs. It still require pairing each parameter input with a parameter name. Here's a tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM