第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將整數(shù)數(shù)組傳遞給ASP.NETWebAPI?

將整數(shù)數(shù)組傳遞給ASP.NETWebAPI?

交互式愛情 2019-07-03 14:16:14
將整數(shù)數(shù)組傳遞給ASP.NETWebAPI?我有一個ASP.NETWebAPI(Version 4)REST服務,在這里我需要傳遞一個整數(shù)數(shù)組。以下是我的行動方法:public IEnumerable<Category> GetCategories(int[] categoryIds){// code to retrieve categories from database}這是我嘗試過的網(wǎng)址:/Categories?categoryids=1,2,3,4
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻1827條經(jīng)驗 獲得超9個贊

你只需要加上[FromUri]在參數(shù)之前,如下所示:

GetCategories([FromUri] int[] categoryIds)

并發(fā)出請求:

/Categories?categoryids=1&categoryids=2&categoryids=3


查看完整回答
反對 回復 2019-07-03
?
DIEA

TA貢獻1820條經(jīng)驗 獲得超3個贊

菲利普W指出,您可能不得不使用這樣的自定義模型綁定器(修改為綁定到實際類型的param):

public IEnumerable<Category> GetCategories([ModelBinder(typeof(CommaDelimitedArrayModelBinder))]long[] categoryIds) {
    // do your thing}public class CommaDelimitedArrayModelBinder : IModelBinder{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        var key = bindingContext.ModelName;
        var val = bindingContext.ValueProvider.GetValue(key);
        if (val != null)
        {
            var s = val.AttemptedValue;
            if (s != null)
            {
                var elementType = bindingContext.ModelType.GetElementType();
                var converter = TypeDescriptor.GetConverter(elementType);
                var values = Array.ConvertAll(s.Split(new[] { ","},StringSplitOptions.RemoveEmptyEntries),
                    x => { return converter.ConvertFromString(x != null ? x.Trim() : x); });

                var typedValues = Array.CreateInstance(elementType, values.Length);

                values.CopyTo(typedValues, 0);

                bindingContext.Model = typedValues;
            }
            else
            {
                // change this line to null if you prefer nulls to empty arrays 
                bindingContext.Model = Array.CreateInstance(bindingContext.ModelType.GetElementType(), 0);
            }
            return true;
        }
        return false;
    }}

然后你可以說:

/Categories?categoryids=1,2,3,4而ASP.NETWebAPI將正確地綁定categoryIds陣列。


查看完整回答
反對 回復 2019-07-03
  • 3 回答
  • 0 關注
  • 1307 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號