ABP (modern ASP. NET template Development Framework) series 20, ABP presentation Layer--dynamic generation of WEBAPI

Source: Internet
Author: User
Tags app service

Click here to go to the ABP series articles General Catalogue

ABP (modern ASP. NET template Development Framework) series 20, ABP presentation Layer--dynamic generation of WEBAPI

The ABP is "ASP. Boilerplate Project (ASP. NET Template project) "for short.

ABP's official website :http://www.aspnetboilerplate.com

ABP's Open source project on GitHub : https://github.com/aspnetboilerplate

Establishing a dynamic WEBAPI controller

The ABP framework is able to automatically generate Web APIs through the application layer:

     Public Interface Itaskappservice:iapplicationservice    {        gettasksoutput gettasks (gettasksinput input);         void updatetask (updatetaskinput input);         void createtask (createtaskinput input);    }

The ABP framework can automatically and dynamically build a Web API controller for the application layer with a single line of key code configuration:

DynamicApiControllerBuilder.For<ITaskAppService>("tasksystem/task").Build();

That's OK! The built-in WEBAPI Controller (/api/services/tasksystem/task) all the methods can be called on the client. The WEBAPI controller is typically configured when the module is initialized. Itaskappservice is an application-layer service (application) interface, and we encapsulate the interface to implement an API controller. Itaskappservice is not limited to the use of application-level services, it is just our customary and recommended way of using it. Tasksystem/task is the namespace of the API controller. In general, you should define at least one layer of namespaces, such as: Company name/application/namespace/namespace 1/service name. ' api/services/' is the prefix for all dynamic Web APIs. So the address of the API controller is generally such a drop: '/api/services/tasksystem/task ', the address of the Gettasks method is generally such a drop: '/api/services/tasksystem/task/gettasks '. Because in the traditional JS is the use of camel-named method, here is not the same. You can also delete an API method, as follows:

Dynamicapicontrollerbuilder    . for <ITaskAppService> ("tasksystem/taskservice")    . Formethod ("createtask"). Dontcreateaction ()    . Build ();

The ForAll method of building multiple API controllers in the application service layer of a program can be tedious, and Dynamicapicontrollerbuilper provides a way to build all of the application-tier services as follows:

    Dynamicapicontrollerbuilder    . ForAll<IApplicationService> (assembly.getassembly (typeof"tasksystem" )    . Build ();

The ForAll method is a generic interface, the first parameter is a collection derived from a given interface, and the last parameter is the prefix of the Services namespace. The ForAll collection has Itaskappservice and Ipersonappservice interfaces. According to the configuration above, the routing of the Service layer is this: '/api/services/tasksystem/task ' and '/api/services/tasksystem/person '.

Service naming convention: the suffix of the service name +appservice (in this case, Person+appservice) is automatically deleted, and the generated WEBAPI controller is named "Person". At the same time, the name of the service will be named by Peak Camel. If you don't like the Convention, you can also customize the name by using the "Withservicename" method. If you don't want to create all of the app service tiers, you can use where to filter some of the services.

Using dynamic JavaScript proxies

You can create Web API controllers dynamically through AJAX. The ABP framework simplifies the creation of Web API controllers with dynamic JS proxies, and you can invoke the Web API controller dynamically via JS

    abp.services.tasksystem.task.getTasks ({    1    }). Done (function (data) {     // Use data.tasks here.    });

The JS Proxy is created dynamically and you need to add a reference to the page:

    <script src="/API/ABP. Serviceproxies/getall "type="text/javascript"></script >

Service methods (see deferred of JQ), service methods use the ABP framework. Ajax instead, you can handle and display errors.

Ajax parameters

Parameters for customizing the Ajax proxy method:

  Abp.services.tasksystem.task.createTask ({assignedpersonid:  3   "  a New Task Description ...    },{ // overrid E JQuery ' s ajax parameters  async : false
      }). Done (function () {Abp.notify.success (  successfully created a task!       ); });

All of the Jq.ajax parameters are valid.

Single Service script

'/api/abpserviceproxies/getall ' will generate all the agents in a file, through '/api/abpserviceproxies/get?name=servicename ' you can also generate a single service proxy, On the page, add:

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

The ABP framework is capable of exposing dynamic API controllers as Angularjs services, as follows:

(function () {Angular.module ('app'). Controller ('Tasklistcontroller', [            '$scope','Abp.services.tasksystem.task', function ($scope, taskservice) {varVM = This; Vm.tasks= []; Taskservice.gettasks ({state:0}). Success (function (data) {Vm.tasks=Data.tasks;            });    }        ]); })();

We can inject the name into the service, and then call this service, just as you would call a normal JS function. Note: After we successfully registered the handler, he was like a augular $http service. The ABP framework uses the $http service of the angular framework, and if you want to configure it through $http, you can set a configuration object as a parameter to the service method.

To use an automatically generated service, you need to add:

    <script src="~/abp framework/framework/scripts/libs/angularjs/abp Framework.ng.js" ></script>    <script src="~/api/abp framework/serviceproxies/getall?type=angular "></script>

Durandal support

The ABP framework can inject services into the Durandal framework, as follows:

    define (['service!tasksystem/task'],    function (taskservice) {        //  Taskservice can usedhere    });

The ABP framework configures Durandal (actually require.js) to parse the service proxy and inject the appropriate JS to the service proxy.

I hope that more domestic architects will be able to focus on the ABP project, and perhaps it will help you, perhaps with your participation, this project can develop better.

Welcome to add ABP Architecture Design Exchange QQ Group: 134710707

Click here to go to the ABP series articles General Catalogue

ABP (modern ASP. NET template Development Framework) series 20, ABP presentation Layer--dynamic generation of WEBAPI

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.