参数绑定ASP.NET Web API中的嵌套对象

时间:2013-12-20 15:54:54

标签: c# asp.net asp.net-mvc asp.net-web-api

我的项目中有以下模型类:

public class RiskInfo
{
    /// <summary>
    /// Gets or sets the risk id.
    /// </summary>
    public string RiskId { get; set; }

    /// <summary>
    /// Gets or sets the risk address.
    /// </summary>
    public PropertyAddress RiskAddress { get; set; }
}

public class PropertyAddress
{
    /// <summary>
    /// Gets or sets the street number.
    /// </summary>
    public string StreetNumber { get; set; }

    /// <summary>
    /// Gets or sets the name of the street.
    /// </summary>
    public string StreetName { get; set; }

    /// <summary>
    /// Gets or sets the city.
    /// </summary>
    public string City { get; set; }

    /// <summary>
    /// Gets or sets the county.
    /// </summary>
    public string County { get; set; }

    /// <summary>
    /// Gets or sets the state.
    /// </summary>
    public string State { get; set; }
}

我的控制器中有一个动作方法,定义如下:

    [GET("hydrantflow/riskinfo")]
    public HttpResponseMessage GetFlowTestByRiskInfo([FromUri]RiskInfo riskInfo)

我试图通过使用此URL从Fiddler调用此操作方法:

http://localhost:63932/hydrantflow/riskinfo?riskid=49WY99000191&streetnumber=900&streetname=pine%20st&city=pinedale&state=wy

但是当我调试我的action方法时,我看到只填充了riskInfo.RiskId属性,riskInfo.RiskAddress保持为null。但是,如果我将上述网址更改为

http://localhost:63932/hydrantflow/riskinfo?riskid=49WY99000191&riskaddress.streetnumber=900&streetname=pine%20st&city=pinedale&state=wy

我看到riskInfo.RiskAddress.StreetNumber现在是900.似乎我需要在我的URL中使用属性名称“riskaddress”的所有参数作为前缀,这似乎不是最好的方法。

使用嵌套对象的参数绑定是否更好?

由于

0 个答案:

没有答案