【问题标题】:can the parameters FromUri be read from an attribute?可以从属性中读取参数 FromUri 吗?
【发布时间】:2016-08-21 10:36:16
【问题描述】:

我有一个简单的控制器,可以接受来自支付系统的响应。

        public async Task<IHttpActionResult> Pending([FromUri] DepositResponse response)
        {
            Logger.LogInfo(JsonConvert.SerializeObject(response));

            return Ok(response);
        }

然而,存款响应具有非常丑陋和不标准化的参数。我无法控制,因为这是支付系统发送的。

public class DepositResponse
{
    public string ppp_status { get; set; }

    public string ExErrorCode { get; set; }

    public string PPP_TransactionID { get; set; }

    public string merchant_site_id { get; set; }

    //etc
}

因此,Resharper 抱怨所选名称不符合规则,我想更改它以匹配项目其余部分中的所有类。

是否有我可以使用的属性,或者我可以创建一个属性以使FromUri 理解响应?

例如

public class DepositResponse
{
    [FromUriName("ppp_status")]
    public string pppStatus { get; set; }

    [FromUriName("ExErrorCode")]
    public string exErrorCode { get; set; }

    [FromUriName("PPP_TransactionID")]
    public string pppTransactionId { get; set; }

    [FromUriName("merchant_site_id")]
    public string merchantSiteId { get; set; }

    //etc
}

我在网上找不到这样的例子,但我想它在处理发送垃圾的外部系统时会非常有用......

有什么想法吗?

【问题讨论】:

    标签: c#-4.0 asp.net-web-api custom-attributes


    【解决方案1】:

    您可以使用以下模型

    public class DepositResponse
        {
    
        [JsonProperty(PropertyName="ppp_status")]
        public string pppStatus { get; set; }
    
        [JsonProperty(PropertyName="ExErrorCode")]
        public string exErrorCode { get; set; }
    
        [JsonProperty(PropertyName="PPP_TransactionID")]
        public string pppTransactionId { get; set; }
    
        [PropertyName(PropertyName="merchant_site_id")]
        public string merchantSiteId { get; set; }
    
        //etc
        }
    

    【讨论】:

    • 这当然行不通。你试过了吗?我正在做一个 HTTP Get,所以参数需要与属性的名称完全匹配。我的解决方案将涉及一些自定义活页夹,或“FromUri”的扩展。
    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 2016-05-21
    • 1970-01-01
    • 2013-02-07
    • 2011-04-24
    • 2017-10-08
    • 1970-01-01
    • 2014-06-11
    相关资源
    最近更新 更多