asp.net mvc api 异常捕捉

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)

你可能感兴趣的:(asp.net)