如何在TypeScript中调用VB类的UserTeDrejta函数(禁用WebMethod)
如何在TypeScript中调用VB类的UserTeDrejta函数(禁用WebMethod)
看起来你现在需要在前端TypeScript里调用VB后端的UserTeDrejta静态方法,还不能用WebMethod,我来给你梳理两种可行的实现方案,其中一种更贴合你原有的代码逻辑,另一种是更安全易维护的优化方案:
方案一:沿用原VB函数返回JS代码的逻辑
因为不能用WebMethod直接通过AJAX调用后端方法,我们可以在页面加载时让后端提前执行UserTeDrejta,并把结果注入到前端全局变量中,前端直接读取这个变量使用。
1. 后端(ASPX页面/代码后置)调用VB函数并注册全局变量
在你的ASPX页面的代码后置文件(或者页面内的<script runat="server">块)中,在页面加载时调用UserTeDrejta,并将结果注册为前端可访问的全局变量:
Protected Overrides Sub OnLoad(e As EventArgs) MyBase.OnLoad(e) If Not IsPostBack AndAlso Session("UserID") IsNot Nothing Then ' 获取登录用户ID Dim userId As Integer = Integer.Parse(Session("UserID").ToString()) ' 调用VB类的静态方法,传入权限规则 Dim permissionScriptContent As String = YourVBClassName.UserTeDrejta("Modifiko_Perdorues", userId) ' 将结果注册为前端全局变量window.userPermission ClientScript.RegisterStartupScript( Me.GetType(), "PermissionScript", $"window.userPermission = {permissionScriptContent};", True ) End If End Sub
注意:把
YourVBClassName替换为实际包含UserTeDrejta方法的VB类名称。
2. 前端TypeScript代码修改
在你的populateUsersTable函数中,读取全局变量userPermission,并将其转换为可执行的逻辑,动态生成表格单元格:
// 声明全局变量类型,避免TypeScript类型报错 declare global { interface Window { userPermission: string; } } function populateUsersTable(users: any[]) { const tableBody = document.querySelector("#perdorues tbody"); if (!tableBody) return; tableBody.innerHTML = ''; // 清空现有行 // 解析后端返回的权限相关JS代码 const permissionCode = window.userPermission || ''; // 将代码包装为可接收user和row参数的函数 const renderPermissionCells = new Function('user', 'row', permissionCode); users.forEach(user => { const row = document.createElement("tr"); if (permissionCode) { // 有权限:执行后端返回的代码,添加ID和Username单元格 renderPermissionCells(user, row); } else { // 无权限:添加占位提示或隐藏相关列 const noPermCell = document.createElement("td"); noPermCell.textContent = "无操作权限"; row.appendChild(noPermCell); } // 这里可以继续添加其他表格列(如邮箱、创建时间等)... tableBody.appendChild(row); }); }
方案二:更安全的权限标记方案(推荐)
原VB函数直接返回JS代码存在XSS安全风险,且不易维护。更优的方式是让后端返回布尔值权限标记,前端自己控制渲染逻辑:
1. 修改VB函数返回布尔值
Public Shared Function UserTeDrejta(rule As String, userid As Integer) As Boolean Dim aa As New DefaultDataSetTableAdapters.QueriesTableAdapter Try Dim result As Integer = aa.CheckakaTeDrejte(rule, userid) ' 直接返回是否有权限的布尔值 Return result > 0 Catch ex As Exception Return False End Try End Function
2. 后端注册权限标记全局变量
Protected Overrides Sub OnLoad(e As EventArgs) MyBase.OnLoad(e) If Not IsPostBack AndAlso Session("UserID") IsNot Nothing Then Dim userId As Integer = Integer.Parse(Session("UserID").ToString()) Dim hasModifyPermission As Boolean = YourVBClassName.UserTeDrejta("Modifiko_Perdorues", userId) ' 注册全局变量window.hasModifyPermission ClientScript.RegisterStartupScript( Me.GetType(), "PermissionFlag", $"window.hasModifyPermission = {If(hasModifyPermission, "true", "false")};", True ) End If End Sub
3. 前端TypeScript逻辑优化
declare global { interface Window { hasModifyPermission: boolean; } } function populateUsersTable(users: any[]) { const tableBody = document.querySelector("#perdorues tbody"); if (!tableBody) return; tableBody.innerHTML = ''; users.forEach(user => { const row = document.createElement("tr"); if (window.hasModifyPermission) { // 有权限:手动添加ID和Username单元格 row.dataset.userId = user.UserID; // ID列 const idCell = document.createElement("td"); idCell.textContent = user.UserID; row.appendChild(idCell); // 用户名列 const usernameCell = document.createElement("td"); usernameCell.textContent = user.Username; row.appendChild(usernameCell); } else { // 无权限:添加无权限提示或隐藏操作列 const noPermCell = document.createElement("td"); noPermCell.colSpan = 2; // 对应ID和用户名两列的宽度 noPermCell.textContent = "无修改用户权限"; row.appendChild(noPermCell); } // 继续添加其他表格列... tableBody.appendChild(row); }); }
关键注意事项
- 确保
UserTeDrejta方法所在的VB类是Public的,且ASPX页面的代码可以访问到该类(比如在同一个命名空间,或已添加引用)。 - 处理
Session("UserID")为空的情况,避免转换Integer时出现异常。 - 方案二更推荐,因为将权限逻辑与渲染逻辑分离,更易维护且避免XSS安全风险。
内容来源于stack exchange
相关产品推荐
相关产品推荐

