Generating a JS-client based on a ASP.NET WebAPI Controller

后端 未结 3 1582
忘掉有多难
忘掉有多难 2021-01-30 11:41

In modern web-projects that use RESTful API\'s we often see AJAX-calls like the one below littered around our JavaScript-files.

$.ajax({
    type: \"POST\",
             


        
3条回答
  •  旧巷少年郎
    2021-01-30 12:10

    This excellent another project allows you to do what you asked for. This poject auto-generates JavaScript proxies for MVC and WebApi controllers. And, this project covers WebApi features such as custom ActionName attributes. With this project, you will have also the Intellisense.
    http://jsnet.codeplex.com/

    Example of Intellisense

    window.test = function test() {
    /// 
    ///This example works.
    ///You have the Intellisense. It's great!!!
    ///No hard coded url.
    ///
    
    //-- 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) {
        /// Response of ajax request
    
        //-- 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);
    
    });
    }
    

提交回复
热议问题