【问题标题】:Web API resource usages in frontend前端的 Web API 资源使用情况
【发布时间】:2015-10-21 23:28:34
【问题描述】:

我遇到过几次这个问题。我更改资源的结果或参数,然后搜索它的使用位置并进行更改。但我经常会错过应用程序中一些晦涩的部分。

什么是查找 API 资源(端点)在例如 Angular 应用程序中使用的所有位置的好方法?

它并不总是像搜索“api/foo/bar/baz”那么简单。 URI 可以连接多个变量。

【问题讨论】:

    标签: javascript angularjs asp.net-web-api static-analysis asp.net-web-api-routing


    【解决方案1】:

    我的方法是使用自定义 Angular 服务并将所有端点信息与其他应用程序特定设置一起存储在那里:

    angular.module("appModule").value("appSettings", {
        title: "My application",
        version: "0.0.0.1",
        webApiHost: "http://localhost:33000/",
        customersEndpoint: "api/customers",
    });
    

    这样您就可以通过在代码中查找appSettings.customersEndpoint 字符串来找到所有引用。这是客户端示例:

    (function (angular) {
        var customersFactory = function ($http, appSettings) {
            var factory = {};
    
            factory.getAll = function () {
                return $http.get(appSettings.webApiHost + appSettings.customersEndpoint);
            }        
    
            return factory;
        };
    
        customersFactory.$inject = ["$http", "appSettings"];
        angular.module("appModule").factory("customersFactory", customersFactory);
    }(angular));
    

    但是,例如,没有什么可以阻止您(或其他人)忽略该技术并通过使用字符串连接来混淆端点。所以,这样的事情没有灵丹妙药——请小心!

    【讨论】:

    • 所以实际上它归结为架构 :) - 这并不是一件坏事
    猜你喜欢
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多