2015-08-26 48 views
0

我正在修復使用現有web api數據的mvc web前端。
網頁API調用有7或8個不同的網址,貫穿控制器。在MVC web應用程序中組織web api URL

我打算用變量替換字符串,我想有一箇中心位置來配置它,所以一旦我把Web API放回到服務器上,我只需要在一個地方更改URL變量,而不是通過控制器搜索。

這是否有一個公約呢?如果沒有,網絡配置是否是最好的選擇?

回答

1

如果是我,我會創建一個代理類來處理Web API。這將爲您提供一個集中URI並隱藏詳細信息(如處理http,身份驗證,序列化等)直接使用API​​的位置。

class SomeApi { 
    // or read this from config 
    const string baseUrl = "https://some-site/api"; 

    public Task<Customer> GetCustomerAsync(int customerId) { 
    // implement me 
    } 
} 

// consumers don't have to know anything about urls, http, etc. 
var api = new SomeApi(); 
var customer = await GetCustomerAsync(1234); 
相關問題