WebApi开发框架:使用HttpGet方式调用WebAPI接口
作者:C/S框架网|www.cscode.ne  发布日期:2020-12-02 15:46:43
  WebApi开发框架:使用HttpGet方式调用WebAPI接口

在DoController控制器添加一个接口方法,注意接口的参数对象是采用Uri参数转换, 定义[FromUri]特性。


C# Code:

/// <summary>
/// 通过URL获取OSS图片
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet]
[ActionName(
"ossimg")]
public IUserResponse DoGetOSSImage([FromUri] ModelAliOSSGet request)
{
  
if (request == null || request.Apikey != "ZD51C485-B611-451C-915F-4BA3900E7CAC")
  
return new ModelResponse { Code = -1, Message = "请求参数错误!" };
  
else
  {
    
string imgUrl = ImageHelper.getImageUrl(request.Phone, request.ImageType, "");
    
return new ModelResponse
    {
      Code
= 0,
      Data
= imgUrl,//返回图片url地址
      
Message = "",
      };
    }
  }
  
  
//来源:C/S框架网 | www.csframework.com | QQ:23404761




接口参数:


C# Code:

public class ModelAliOSSGet
{
  
public string Apikey { get; set; }
  
  
public string Phone { get; set; }
  
  
public string ImageType { get; set; }
  
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761



测试接口:


C# Code:

ModelAliOSSGet model = new ModelAliOSSGet
{
  Phone
= "15846647518",
  ImageType
= "carimage",
  Apikey
= "ZD51C485-B611-451C-915F-4BA3900E7CAC"
  };
  

  
string url = "http://localhost:16788/api/do/ossimg";
  
  
//序列化实体对象,把属性转换为URL参数字符串
  
string param = WebApiTools.GetUrlParam(model);
  
    //HttpGet方式调用webapi接口
  
string result = WebApiTools.Get(url, param);
  
  
  
//来源:C/S框架网 | www.csframework.com | QQ:23404761






C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务



上一篇 下一篇