Web API バックエンドを使用する angularJS サイトがあります。保存しようとしていますが、以下に定義されている結果タイプの列挙型が返されることを期待しています
public enum ResultType
{
Success,
InvalidAccountNumber,
InvalidAddress,
NotFound,
Incomplete,
Error,
Unauthorized
}
リソースをセットアップするために使用したコードを次に示します。
app.factory('NotificationOptInApi', function ($resource) {
return $resource(API_SOURCE + 'NotificationOptIn', {}, {
'save': { method: 'POST', isArray: true }
});
});
これが保存するための私の呼び出しです
$scope.saveNotifications = function () {
var result = NotificationOptInApi.save($scope.notifications, function () {
// check for failure
if (result !== 'Success') {
$scope.saveError = result;
return;
}
});
};
これが私が返すものです
{0: "S", 1: "u", 2: "c", 3: "c", 4: "e", 5: "s", 6: "s", $get: function, $save: function, $query: function, $remove: function, $delete: function}
これを返すコードは次のとおりです。これは、reusltType の代わりにリストを返そうとしたときです。
public List<ResultType> Post([FromBody]dynamic notificationSubscriptionHolder)
{
List<NotificationSubscriptionData> notificationSubscriptions = notificationSubscriptionHolder.ToObject<List<NotificationSubscriptionData>>();
Logger.Info("Update Account Notifications", string.Format("<updateAccountNotificationsToUser><NotificationSubscriptionData>{0}</NotificationSubscriptionData><user>{1}</user></updateAccountNotificationsToUser>", notificationSubscriptions, this.CurrentIdentity.Name));
try
{
return _accountManager.UpdateAccountNotifications(this.CurrentIdentity.Name, notificationSubscriptions);
}
catch(Exception ex)
{
return new List<ResultType>() { ResultType.Error };
}
}
これは問題なく他の場所で機能しました。結果を確認でき、「成功」となります。ここでわかる違いは、配列を保存しているため、IsArray を true に設定する必要があったことです。現在、保存している配列に1つのアイテムがあります。私が得る応答は文字の配列です。それらを合わせて「成功」と綴ります。
配列を保存しているにもかかわらず、応答を配列として返さないようにする方法はありますか? また、配列を返す必要があると考えてリストを返そうとしました。これを行うと、1 つの項目を含む配列が得られますが、その 1 つの項目内に、Success を表す文字の配列があります。