The Dynamic Web API layer for ASP.

Source: Internet
Author: User
Tags app service

Building a Dynamic Web API controller

The ASP. NET boilerplate can automatically generate the Web API layer for your application layer. For example, we have one of the following application services:

1  Public Interface Itaskappservice:iapplicationservice 2 {3     gettasksoutput gettasks (gettasksinput input); 4     void updatetask (updatetaskinput input); 5     void createtask (createtaskinput input); 6 }

We want this service to be exposed as a Web API controller to facilitate client invocation. The ASP. NET boilerplate can automatically and dynamically create a Web API controller for this application service, which is done with a single line of configuration code.

Dynamicapicontrollerbuilder.for<itaskappservice> ("tasksystem/task"). Build ();

This is almost all code! An API controller is created with the address "/api/services/tasksystem/task", and all methods are available at the client. This configuration is usually performed when the model is initialized.

    Itaskappservice is an application service that we want to package into API controllers, which is not strictly limited, but is a common and recommended practice. "Tasksystem/task" is the name of the API controller's namespace, and the name can be arbitrary. You can define at least one hierarchy of namespaces, or you can define more levels of namespaces, such as "Mycompany/myapplication/mynamespace1/mynamespace2/myservicename". "/api/services/" is the prefix for all dynamic Web API controllers. Therefore, the address of the API controller will be: "/api/services/tasksystem/task", while the address of the Gettasks method is "/api/services/tasksystem/task/gettasks". The method name is automatically converted to the Camel rule (lowercase), which is the traditional practice of JavaScript code.
    You can also exclude certain methods of the app service from the API methods, such as you can define as follows:

1 Dynamicapicontrollerbuilder 2 . For<itaskappservice> ("tasksystem/taskservice")3 . Formethod ("createtask"). Dontcreateaction ()4 . Build ();
ForAll method

In an application, we may have a lot of application services, then one by one to build the API will be a tedious and easy to forget the work.
Dynamicapicontrollerbuilder provides a way for a Web API controller to be established for all application services once a call is made:

1 Dynamicapicontrollerbuilder 2 . Forall<iapplicationservice> (assembly.getassembly (typeof"tasksystem" )3 . Build ();

ForAll is a generic method, and a generic type is an interface. The first parameter is the assembly, which contains classes that implement the given interface. The last interface is the namespace prefix for the service. For example, in the assembly we have Itaskappservice and ipersonappservice two interfaces. Depending on the configuration, the service will be "/api/services/tasksystem/task" and "/api/services/tasksystem/person". To generate the service name, the framework automatically removes service and appservice suffixes and I prefixes from the interface name. The service name is also automatically converted to a camel casing rule. If you don't like the conversion, you can use the "withservicename" method so that you can decide the name yourself. This is very helpful for building API controllers for most application services.

Using dynamic JavaScript proxies

You can use these dynamically created Web APIs in JavaScript's Ajax methods. ASP. NET boilerplate also provides an easier way: Create dynamic JavaScript proxies for dynamic Web API controllers. As a result, you can invoke a Dynamic Web API method in JavaScript like calling a function:

1 abp.services.tasksystem.task.getTasks ({2     13}). Done (function (data) {  4     //usedata.tasks here ... 5 });

JavaScript proxies are created dynamically, and you need to include dynamic scripts in the page before using them:

<src= "/api/abpserviceproxies/getall"  type= "Text/javascript"  ></script>

The service method returns a commitment (view jquery.deferred). You can implement "finish", "Fail" and "callback" through registration, and so on. The service method contains Abp.ajax, which handle errors and display error messages when needed.

Ajax parameters

You may want to transfer the custom AJAX parameters into the proxy method. You can transfer the second parameter to achieve the following:

1 Abp.services.tasksystem.task.createTask ({2Assignedpersonid:3,3Description'a new task description ...'4},{//ajax parameters that overwrite jquery5     Async:false,6Timeout300007 }). Done (function () {8Abp.notify.success ('successfully created a task!');9});

All Jquery.ajax parameters are valid here.

Standalone service script

"/api/abpserviceproxies/getall" generates all the service proxies in a file. You can also use "/api/abpserviceproxies/get?name=servicename" to generate a separate service proxy, just include the following script code in the page:

<src= "/api/abpserviceproxies/get?name=tasksystem/task"  type= " Text/javascript "></script>
Angular Support

The ASP. NET boilerplate can expose dynamic API controllers as Angularjs services. As in the following example:

1(function() {2Angular.module (' app '). Controller (' Tasklistcontroller ', [3' $scope ', ' abp.services.tasksystem.task ',4         function($scope, taskservice) {5            varVM = This;6Vm.tasks = [];7            Taskservice.gettasks ({8state:09}). Success (function(data) {TenVm.tasks =Data.tasks; One      }); A     } -    ]); -})();

We can inject a service with a name (with namespace). Then we can call it the same way as a normal JavaScript method. It is important to note that since this is like the $http service in angular, we have registered a success handler (instead of done). The ASP. Boilerplate uses the Angularjs $http service. If you want to pass the $http configuration, you can pass a configuration object as the last parameter of the service method.

In order to be able to use the automatically generated service, you need to include the necessary script in the page:

1 <  src= "~/abp/framework/scripts/libs/angularjs/abp.ng.js"></Script  >2<src= "~/api/abpserviceproxies/getall?type= Angular "></script>
Durandal support

ASP. NET boilerplate supports the injection of service proxies in the Durandal application. See an example of a view model:

1 define ([' Service!tasksystem/task '],2     function (taskservice) {3       //taskservice can be used here 4 });

ASP. Boilerplate by configuring Durandal (actually require.js) to parse "service!" prefix, and inject the appropriate JavaScript service proxy.

This article is translated from ASP. Boilerplate official Address: Http://www.aspnetboilerplate.com/Pages/Documents/Dynamic-Web-API

The Dynamic Web API layer for ASP.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.