[英]application/business logic behind web api and validation error in UI?
I understand that "service" layer where I have my business/application logic should be behind "web api" layer.我知道我拥有业务/应用程序逻辑的“服务”层应该在“web api”层之后。 Here I have my "web api" layer which have CRUD methods.在这里,我有我的“web api”层,其中包含 CRUD 方法。
So, if I want to add the application/business logic, for example, before add new employee, I want to check to make sure employee name
must be unique, I shall do so as below in my web api PostEmployee
method?所以,如果我想添加应用程序/业务逻辑,例如,在添加新员工之前,我想检查以确保employee name
必须是唯一的,我应该在我的 web api PostEmployee
方法中这样做吗?
namespace WebApi.Controllers
{
public class EmployeeController : ApiController
{
private AppDbContext db = new AppDbContext();
// POST api/Employee
[ResponseType(typeof(Employee))]
public IHttpActionResult PostEmployee(Employee employee)
{
// application / business logic to put here, right?
db.Employees.Add(employee);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
}
}
}
In my UI, I use Console project which has this add employee method and application / business logic validation error to show here as shown?在我的 UI 中,我使用具有此添加员工方法和应用程序/业务逻辑验证错误的控制台项目在此处显示,如图所示?
static async Task AddEmployee()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(webAPIURL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var employee = new Employee();
//POST Method
Console.Write("Enter your name: ");
employee.Name = Console.ReadLine();
Console.Write("Enter your position: ");
employee.Position = Console.ReadLine();
Console.Write("Enter your age: ");
employee.Age = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter your salary: ");
employee.Salary = Convert.ToInt16(Console.ReadLine());
HttpResponseMessage responsePost = await client.PostAsJsonAsync("api/Employee", employee);
if (responsePost.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri returnUrl = responsePost.Headers.Location;
if (returnUrl != null)
{
Console.WriteLine("Employee data successfully added.");
}
//Console.WriteLine(returnUrl);
}
else
{
// application / business logic validation error to show here?
Console.WriteLine("Internal server Error");
}
}
}
Found partial solution (below).找到部分解决方案(如下)。 But how to print the exception in console ui?但是如何在控制台 ui 中打印异常?
// POST api/Employee
[ResponseType(typeof(Employee))]
public IHttpActionResult PostEmployee(Employee employee)
{
var duplicateName = db.Employees.Where(b=>b.Name == employee.Name).SingleOrDefault();
if (duplicateName != null)
{
var msg = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("Duplicate employee name found. ")),
ReasonPhrase = "Duplicate"
};
throw new HttpResponseException(msg);
}
db.Employees.Add(employee);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
}
If you have to get the error message from the HttpResponseMessage
you have to get the HttpError object as shown below.如果必须从HttpResponseMessage
获取错误消息,则必须获取 HttpError 对象,如下所示。 The HttpError
object then contains the ExceptionMessage
, ExceptionType
, and StackTrace
information:然后HttpError
对象包含ExceptionMessage
、 ExceptionType
和StackTrace
信息:
if (responsePost.IsSuccessStatusCode)
{
// Get the URI of the created resource.
Uri returnUrl = responsePost.Headers.Location;
if (returnUrl != null)
{
Console.WriteLine("Employee data successfully added.");
}
//Console.WriteLine(returnUrl);
}
else
{
// application / business logic validation error to show here?
HttpError error = responsePost.Content.ReadAsAsync<HttpError>().Result;
Console.WriteLine("Internal server Error: "+error.ExceptionMessage);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.