动态We API层(动态生成js)
ABP动态webapi前端怎么调用?
研究abp项目时,页面js文件中一直不明白abp.services... 是从哪里来的
在调试SimpleTaskSystem的AngularJs demo时,一开始我只看到对服务的应用。
|
1
2
3
4
5
|
app.controller(controllerId, ['$scope', 'abp.services.tasksystem.task',function($scope, taskService){}]); |
在查找源代码中的所有js文件后还是没找到abp.services.tasksystem.task的定义,那么现在就剩下最后一种情况。这些服务是系统生成的,这样的话与动态WebApi的设计思路也是一致的。在layout.cshtml中有两处js引用
|
1
2
3
|
<script src="~/api/AbpServiceProxies/GetAll?type=angular"></script><script src="~/AbpScripts/GetScripts" type="text/javascript"></script> |
生成所有服务
~/api/AbpServiceProxies/GetAll?type=angular 对应的就是就是Abp对系统所有服务生成的JavaScript,现在对url进行反推我们可以在Abp.Web.Api中找到AbpServiceProxiesController,其中有一ScriptProxyManager 类型的字段_scriptProxyManager。ScriptProxyManager就是生成所有服务的一管理者。
在AbpServiceProxiesController中的GetAll方法有一参数type。这个参数表示根据什么js框架生成javascript,目前Abp提供了Angular与jQuery两种支持。

在ScriptProxyManager中会根据不同的type调用不同的IScriptProxyGenerator生成javascript代码。以Angular的实现AngularProxyGenerator为例。
public string Generate()
{
var script = new StringBuilder(); script.AppendLine("(function (abp, angular) {");
script.AppendLine("");
script.AppendLine(" if (!angular) {");
script.AppendLine(" return;");
script.AppendLine(" }");
script.AppendLine(" ");
script.AppendLine(" var abpModule = angular.module('abp');");
script.AppendLine(" ");
script.AppendLine(" abpModule.factory('abp.services." + _controllerInfo.ServiceName.Replace("/", ".") + "', [");
script.AppendLine(" '$http', function ($http) {");
script.AppendLine(" return new function () {"); foreach (var methodInfo in _controllerInfo.Actions.Values)
{
var actionWriter = CreateActionScriptWriter(_controllerInfo, methodInfo); script.AppendLine(" this." + methodInfo.ActionName.ToCamelCase() + " = function (" + GenerateJsMethodParameterList(methodInfo.Method) + ") {");
script.AppendLine(" return $http(angular.extend({");
script.AppendLine(" abp: true,");
script.AppendLine(" url: abp.appPath + '" + actionWriter.GetUrl() + "',");
actionWriter.WriteTo(script);
script.AppendLine(" }, httpParams));");
script.AppendLine(" };");
script.AppendLine(" ");
} script.AppendLine(" };");
script.AppendLine(" }");
script.AppendLine(" ]);");
script.AppendLine(); //generate all methods script.AppendLine();
script.AppendLine("})((abp || (abp = {})), (angular || undefined));"); return script.ToString();
}
AngularProxyGenerator对所有的服务与Action进行了扫描生成javascript。
不过将所有服务都返回到客户端,好像并不怎么安全。
另外ScriptProxyManager对生成的javascript代码进行了缓存。
基础配置
~/AbpScripts/GetScripts对应的则是Abp.Web.Mvc下的AbpScriptsController,AbpScriptsController主要提供一些基础的配置信息到客户端。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[DisableAuditing]public async Task<ActionResult> GetScripts(){ var sb = new StringBuilder(); sb.AppendLine(_multiTenancyScriptManager.GetScript()); sb.AppendLine(); sb.AppendLine(_sessionScriptManager.GetScript()); sb.AppendLine(); sb.AppendLine(_localizationScriptManager.GetScript()); sb.AppendLine(); sb.AppendLine(await _authorizationScriptManager.GetScriptAsync()); sb.AppendLine(); sb.AppendLine(await _navigationScriptManager.GetScriptAsync()); sb.AppendLine(); sb.AppendLine(await _settingScriptManager.GetScriptAsync()); sb.AppendLine(GetTriggerScript()); return Content(sb.ToString(), "application/x-javascript", Encoding.UTF8);} |
这些信息分别是:
|
接口 |
实现 |
说明 |
|
IMultiTenancyScriptManager |
MultiTenancyScriptManager |
多租户配置 |
|
ISettingScriptManager |
SettingScriptManager |
Abp基础配置 |
|
INavigationScriptManager |
NavigationScriptManager |
导航信息 |
|
ILocalizationScriptManager |
LocalizationScriptManager |
本地化 |
|
IAuthorizationScriptManager |
AuthorizationScriptManager |
权限 |
|
ISessionScriptManager |
SessionScriptManager |
Session信息 |
动态We API层(动态生成js)的更多相关文章
- ABP框架 - 动态Web Api层
文档目录 本节内容: 创建动态Web Api控制器 ForAll 方法 重写 ForAll ForMethods Http 动词 WithVerb 方法 HTTP 特性 命名约定 Api 浏览器 Re ...
- 动态Web API层
返回总目录 本篇目录 构建动态Web API控制器 ForAll 方法 重写ForAll 方法 Http动词 动态Javascript代理 Ajax参数 单一服务脚本 Angular支持 Durand ...
- DDD开发框架ABP之动态Web API层
建立动态Web API 控制器 ASP.NET Boilerplate 能够自动为您的应用层产生Web API层.比如说我们有如下的一个应用服务: public interface ITaskAppS ...
- ABP官方文档翻译 5.2 动态We API层
动态Web APID层 创建动态Web API控制器 ForAll方法 重写ForAll ForMethods Http动词 WithVerb方法 HTTP特性 命名约定 API管理器 RemoteS ...
- 动态We API(ABP官方文档翻译)
动态Web API层 创建动态Web API控制器 ForAll方法 重写ForAll ForMethods Http动词 WithVerb方法 HTTP特性 命名约定 API管理器 RemoteSe ...
- 开源小工具 - swagger API访问代码生成器(js/typescript)
现在流行前后端分离,后端通常会使用swagger生成api文档,提供给前端的同事来调用,一般前端是查看这个文档,根据文档说明编写调用代码.其实,swagger已经提供了足够多的描述信息,我们可以基于s ...
- ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi
点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerplate Project (ASP.N ...
- ABP展现层——动态生成WebApi
ABP展现层——动态生成WebApi 点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerp ...
- arcgis api 3.x for js 入门开发系列二十一气泡窗口信息动态配置模板
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
随机推荐
- 813. Largest Sum of Averages
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the su ...
- 通过网站统计或系统监视器查看IIS并发连接数
如果要查看IIS连接数,最简单方便的方法是通过“网站统计”来查看,“网站统计”的当前在线人数可以认为是当前IIS连接数;如果要想知道确切的当前网站IIS连接数的话,最有效的方法是通过windows自带 ...
- IdentityServer4 密码模式实现
1. 修改 Config.cs using System.Collections; using System.Collections.Generic; using IdentityServer4.M ...
- Swift 里集合类型协议的关系
  Sequence A type that provides sequential, iterated access to its elements. 是最基础的协议,可以通过迭代来获取它的元素 ...
- 安装vue后报错 bash: vue: command not found
今天参照之前写的vue的环境搭建一个新的项目 http://www.cnblogs.com/stella1024/p/7570884.html 安装完vue,并提示成功: $ npm install ...
- POJ 1062
#include<iostream> #include<stdio.h> #define MAXN 105 #define inf 10000000 using namespa ...
- Storm实现数字累加Demo
import java.util.Map; import backtype.storm.Config; import backtype.storm.LocalCluster; import backt ...
- android studio生成aar包并在其他工程引用aar包
1.aar包是android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android studio 工程后,其他工程可以方便引用源码和资源文件 ...
- 代码阅读——十个C开源项目
1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连 ...
- golang笔记(1)-数据库查询结果映射至结构体
通用的映射模式 query:="select id,name from user where id=?" //单个结构体ret:=&Activity{} DbClient( ...