欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

asp.net mvc api 异常捕捉

程序员文章站 2022-03-04 13:01:27
...
public Task Get(int id)
{
	var task = _taskRepository.Get(id);

	if (task == null)
	{
		throw new HttpResponseException(new HttpResponseMessage
		{
			StatusCode = HttpStatusCode.NotFound,
			Content = new StringContent("Task not found")
		});
	}

	return task;
}
public Task Put(Task task)
{
	try
	{
		task = _taskRepository.Put(task);
	}
	catch (Exception)
	{
		throw new HttpResponseException(new HttpResponseMessage
		{
			StatusCode = HttpStatusCode.NotFound,
			Content = new StringContent("Task not found")
		});
	}

	return task;
}

//HttpStatusCode.NotFound (404)

转载于:https://www.cnblogs.com/fx2008/archive/2012/12/15/2819149.html