I use a custom action filter to handle specific exceptions to my service class to populate the model service I want to, and then return the scene.
For example, take your previous code:
public ActionResult SomeAction (SomeViewModel Models) {try {_someService.SomeMethod (model); } Catch (prior to service expansion) {ModelState.AddModelError (ex.Key, ex.ErrorMessage); } See Return (); } Actually, this would call a service, and if a ServiceException was thrown, it did not know there was any issue w / model data, and add error to ModelState, then Just come back to the scene but I have seen some very non-dry patterns because I had this same try / catch code in every verb method. Therefore, to make it dry up a bit, I originally created a new HandleServiceError action filter:
Public class HandleServiceErrorAttribute: HandleErrorAttribute {Public Override Zero OnException ( ExceptionContext reference) {((Controller) context.Controller) .ModelState .AddModelError ((ServiceException context.Exception) .key, ((service exception) context.exception) .ErrorMessage; Context.ExceptionHandled = True; }} Then simplified the methods of my action like:
public ActionResult SomeAction (SomeViewModel Models) {_someService.SomeMethod (model); See Return (); } The problem is, once the action filters handle the error, it does not return to my methodology, I think, under the hood, why is this happening
Is this possible?
Thanks in advance.
Update: I tried to get suggestions from my answer, She came.
For example, if you look at your Controllers \ ProductController.cs code, they are going to the ModelState of the Controller at that point using a service locator to create an empty manufacturer service of the controller:
public product controller () {_service = new product service (new modelstate on (this.modelstate), new product's compository ()); } But if you look at the injected constructor, then it assumes that the modelstate will be injected into the constructor for the service:
public Product Controller (IP Product Service Service) {_service = service; } I do not know how to get CI to work with the current model's modelstate. If I can understand it, then this method can work.
You can still return related views:
context .Result = new ViewResult {ViewName = context.RouteData.GetRequiredString ("Action")}; You can also see an option about how to verify at the service level.
Comments
Post a Comment