使用HttpClientJsonExtensions的自定义HTTP响应和响应消息

时间:2020-03-11 18:18:02

标签: rest api http http-post blazor

我正在开发使用REST API的Blazor应用,并且正在使用PostJsonAsync方法(具有无效的返回类型):

private fun tryCloseKeyboardAndClearFocusFromEditText(focus: View, ev: MotionEvent?) {
        if (focus is TextInputEditText) {
            val location = IntArray(2)
            (focus as? TextInputEditText)?.let{
                it.getLocationInWindow(location)
                val outRect = Rect(location[0], location[1], location[0] + it.width, location[1] + it.height)
                if (!outRect.contains(ev?.rawX?.toInt() ?: 0, ev?.rawY?.toInt() ?: 0)) {
                    closeKeyboard()
                    it.clearFocus()
                }
            }
        }
    }

在我的控制器中,我可以捕获异常,其中包括我想通过调用返回的消息:

public async Task CreateUnit(UnitEntity unit)
    {
        try
        {
            await _http.PostJsonAsync("api/units", unit);
        }

        catch(Exception e)
        {
            throw;
        }
    }

如果控制器中的方法引发异常(所有异常都有详细说明),则它将无法返回或将其包含在任何响应消息中。

TL; DR我想使PostJsonAsync方法在失败时返回自定义消息。目前是200 OK或500服务器错误,没有详细信息。

0 个答案:

没有答案