asp.net webapi:无法将错误字符串转换为http响应消息

时间:2019-05-22 17:30:08

标签: asp.net-web-api

我正在实现一个asp.net webapi控件,该控件更新一个值并向客户端返回一个字符串。当前我收到错误字符串,无法将字符串转换为http响应消息

[System.Web.Http.HttpPut]
    public HttpResponseMessage ReviewLegalFundClass(int fundClassId, int reviewTypeId)
    {
        var svc = Resolve<IUserIdentityService>();
        var user = svc.GetCurrentUser();

        try
        {
            var fcService = GetService<LEGAL_FUND_CLASS>();
            var fundClass = fcService.Get(fundClassId);
            var response = "N/A";
            var users = GetViewService<V_PERMAL_USER_ALL>().GetAll();

            if (reviewTypeId == (int)LegalFundClassReviewType.Fees)
            {
                fundClass.FEES_LAST_REVIEWED_BY_ID = user.ID;
                fundClass.FEES_LAST_REVIEWED_DATE = DateTime.Now;

                var feeUsr = users.FirstOrDefault(x => x.ID == fundClass.FEES_LAST_REVIEWED_BY_ID);
                response = $"Last Reviewed: {feeUsr?.LOGIN_NAME} {fundClass.FEES_LAST_REVIEWED_DATE.Value.ToString(PermalConstants.DateFormat2)}";
            }
            else if (reviewTypeId == (int)LegalFundClassReviewType.Terms)
            {
                fundClass.TERMS_LAST_REVIEWED_BY_ID = user.ID;
                fundClass.TERMS_LAST_REVIEWED_DATE = DateTime.Now;

                var termsUsr = users.FirstOrDefault(x => x.ID == fundClass.TERMS_LAST_REVIEWED_BY_ID);
                response = $"Last Reviewed: {termsUsr?.LOGIN_NAME} {fundClass.TERMS_LAST_REVIEWED_DATE.Value.ToString(PermalConstants.DateFormat2)}";
            }
            else throw new Exception("Invalid fund class review type");

            fcService.Update(fundClass);

            return response;
        }
        catch (Exception ex)
        {
            return GetError(ex);
        }
    }

1 个答案:

答案 0 :(得分:0)

因为您要在方法中返回一个字符串:您将响应变量声明为字符串“ N / A”。因此,您将需要将'HttpResponseMessage'的返回类型更改为'string'。

public string ReviewLegalFundClass(int fundClassId, int reviewTypeId)

当您发现异常时...也可以尝试

return ex.Message;

throw new Exception(ex.Message);

Exception.Message属性是一个字符串,因此在这里会很不错。或者,如果您的GetError()方法返回一个字符串,则忽略我说的哈哈。祝好运。让我知道这对您有何帮助。