I want to perform validation operations in a modal that enters data in a datatable, but I have not used task before. What should I write where it says *****? Is it right to do it this way?
[HttpPost]
public Task<JsonResult> AddNews( News newss)
{
NewsValidator vn = new NewsValidator();
ValidationResult result = vn.Validate(newss);
if (result.IsValid)
{
var sonuc= _adminService.addnws(newss);
return Task.FromResult(Json(sonuc));
}
else
{
foreach (var item in result.Errors)
{
ModelState.AddModelError(item.PropertyName, item.ErrorMessage);
}
}
return *******();
}
CodePudding user response:
You could implement the IValidatbleObject
interface in your News
object. The asp.net modelbinder will handle validation based on your custom validation logic in the News
object and return 400
if it does not validate. This should also render your NewsValidator
class surplus and thus reduce your code.
In general I would recommend using the build in model validation instead of doing your own. You can read more about it here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0
Also, remember to mark your method as async, and avoid using Task.FromResult. Try to use await
to avoid exceptional situations.
CodePudding user response:
I don't know what the data type of sonuc
is (whatever is returned by _adminService.AddNws()
), so I'll call it ReturnType
. Replace that with what it's actually.
Let's clean this code up a bit.
[HttpPost]
public ActionResult<ReturnType> AddNews(News news)
{
NewsValidator validator = new NewsValidator();
ValidationResult validationResult = vn.Validate(newss);
if (result.IsValid)
{
return _adminService.AddNws(news);
}
foreach (var item in result.Errors)
{
ModelState.AddModelError(item.PropertyName, item.ErrorMessage);
}
return BadRequest(ModelState);
}
To explain the changes:
- The return type should be an
ActionResult<>
, with a generic type being the object you're interested in returning. In this case,ReturnType
. Learn more about ActionResult. - The return type has no reason including
Task
. Task is used to indicate the method is asynchronous. Your method has nothing asynchronous about it.Json()
, which you wrap inTask.FromResult
, is not asynchronous.Task
(andasync/await
keywords) should only be used on non-CPU bound events. Serializing an object into JSON is purely CPU bound, so it's inappropriate to make asynchronous. - There's no reason for an
else
block, because theif
blockreturns
. - Typically, when a client passing in invalid parameters, you use a 400 Bad Request HTTP status code in your result. Learn more about HTTP status codes.
- Use
BadRequest
to apply a 400 HTTP status code easily to the response. ModelState
is aModelStateDictionary
, which means that you can that as the parameter inBadRequest()
to indicate the errors to the client.- Please use sane variables names.
vn
is a reverse acronym for News Validator.result
could mean any type of result.sonuc
, well, I frankly have no idea what that means. - The standard casing for methods in c# is PascalCasing.
_adminService.AddNws
is renamed to match.