In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to dynamically generate WebApi in ASP.NET. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Establishment of dynamic WebApi controller
The Abp framework can automatically generate web api 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 through the configuration of one line of key code:
DynamicApiControllerBuilder.For (tasksystem/task) .Build ()
This is OK! All the methods of the built webapi controller (/ api/services/tasksystem/task) can be called on the client side. The webapi controller is usually configured when the module is initialized. ITaskAppService is an application layer service (application service) interface, and we encapsulate the interface to implement an api controller. ITaskAppService is not limited to the use of application layer services, this is just what we are accustomed to and recommend. Tasksystem/task is the namespace of the api controller. In general, at least one layer of namespace should be defined, such as company name / application / namespace / namespace 1 / service name. 'api/services/' is the prefix for all dynamic web api. So the address of the api controller is usually like this: the address of the'/ api/services/tasksystem/task',GetTasks method is usually like this:'/ api/services/tasksystem/task/getTasks'. Because the hump naming method is used in traditional js, it is also different here. You can also delete an api method, as follows:
DynamicApiControllerBuilder .for ("tasksystem/taskService") .ForMethod ("CreateTask"). DontCreateAction () .build ()
The ForAll method of building multiple api controllers in the application service layer of the program may be boring, and DynamicApiControllerBuilper provides a way to establish all the application layer services, as follows:
DynamicApiControllerBuilder .ForAll (Assembly.GetAssembly (typeof (SimpleTaskSystemApplicationModule)), "tasksystem") .build ()
The ForAll method is a generic interface, with the first parameter being a collection derived from a given interface, and the last parameter being the prefix of the services namespace. The ForAll collection has ITaskAppService and IpersonAppService interfaces. According to the above configuration, the routing of the service layer is like 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 resulting webapi controller is named "person". At the same time, the name of the service will be named by camel. If you don't like this convention, you can also customize the name through the "WithServiceName" method. If you don't want to create all the application service layers, you can use where to filter some of the services.
Using dynamic JavaScript agents
You can create web api controllers dynamically through ajax. The Abp framework simplifies the establishment of web api controllers through dynamic js agents. You can call web api controllers dynamically through js.
Abp.services.tasksystem.task.getTasks ({state: 1}) .done (function (data) {/ / use data.tasks here..})
The js proxy is created dynamically, and a reference needs to be added to the page:
The service method (service methods) returns the convention (see JQ's Deferred). Instead, the service method uses the Abp framework .ajax to handle and display errors.
Ajax parameter
Customize the parameters of the ajax proxy method:
Abp.services.tasksystem.task.createTask ({
AssignedPersonId: 3
Description:'a new task description...'
}, {/ / override jQuery's ajax parameters
Async: false
Timeout: 30000
}) .done (function () {
Abp.notify.success ('successfully created a taskstores')
});
All jq.ajax parameters are valid.
Single service script
'/ api/abpServiceProxies/GetAll' will generate all the proxies in one file, and you can also generate a single service proxy through' / api/abpServiceProxies/Get?name=serviceName', adding to the page:
Augular framework support
The Abp framework can expose dynamic api controllers as angularjs services, as follows:
(function () {
Angular.module ('app'). Controller (' TaskListController', [
'$scope',' abp.services.tasksystem.task'
Function ($scope, taskService) {
Var vm = 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 the service, just as we would call the normal js function. Note: after we successfully register the handler, it is like an 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 automatically generated services, you need to add:
Durandal support
The ABP framework can inject services into the Durandal framework, as follows:
Define (function (taskService) {/ / taskService can be used here})). This is how WebApi is generated dynamically in the ASP.NET shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.