Base solution for your next web application
Open Closed

Exception in controller action #2138


User avatar
0
bowlvan created
[DontWrapResult]
        [AcceptVerbs(HttpVerbs.Post)]
        [AbpMvcAuthorize(AppPermissions.Pages_Master_Location_Delete)]
        public ActionResult Destroy([DataSourceRequest] DataSourceRequest request, LocationViewModel item)
        {
            try
            {
                if (item != null && item.Id.HasValue)
                {
                    _locationManager.Delete(item.Id.Value);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("DeleteError", "error");
            }
            
            return Json(new[] { item }.ToDataSourceResult(request, ModelState));
        }

Response is not json but Asp.net exception instead. Please advice how to resolve.

Server Error in '/' Application.

The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.AppStockAmount_dbo.AppLocation_LocationId". The conflict occurred in database "Demo", table "dbo.AppStockAmount", column 'LocationId'. The statement has been terminated.


13 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Do you use ASP.NET MVC 5.x version or ASP.NET Core version ?

  • User Avatar
    0
    bowlvan created

    I'm using ASP.NET MVC. I've tried to On/Off Custom Errors but always return Error Page when exception occurs.

    Btw, same code has tested on classic MVC project and working. Not sure why error still pops after exception catched and processed. In this case I need to reture error message to client. After action executed, I also traced running into Application_Error in global.ascx.cs. Please help to verify, thanks.

  • User Avatar
    0
    bowlvan created

    I've tried below configuration but still not working.

    Configuration.Modules.AbpMvc().IsValidationEnabledForControllers = false;
    

    btw: My Abp version is 1.0

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Have you tried without [DontWrapResult] attribute ? Can you also share a screenshot of your error page if it does not work ?

  • User Avatar
    0
    bowlvan created

    Yes,i tried but still not working.

    The exception I got is as expected. I try to delete an record which has key constraint. The problem is I can't get a Json result with error message. Always asp.net yellow page html (error page) instead. The same code tested on default ASP.NET MVC project and working. Does any Abp function to intercept error and return error?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you try to add this attribute to your action ?

    [UnitOfWork(IsDisabled = true)]
    

    If that does not work, can you send your project to <a href="mailto:[email protected]">[email protected]</a>.

    Thanks.

  • User Avatar
    0
    bowlvan created

    Anyone can help on this?? I already catch the exception in action why still jump to Abp OnException process??

  • User Avatar
    0
    bowlvan created

    <cite>ismcagdas: </cite> Hi,

    Can you try to add this attribute to your action ?

    [UnitOfWork(IsDisabled = true)]
    

    If that does not work, can you send your project to <a href="mailto:[email protected]">[email protected]</a>.

    Thanks.

    It's working now. So I guess it's due to Post Commit of UOM after action done?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes you are right, Since ABP Controller Actions are UOW by default, your operation is committed after your action's code is executed. Can you try to remove this attribute [UnitOfWork(IsDisabled = true)] and change your Action's return type to JsonResult instead of ActionResult ?

  • User Avatar
    0
    bowlvan created

    Just tried use JsonResult but not working. still got code 500 & yellow page.

    So if I want to use UOM in controller i have to initialize UOM in action instead?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    No actually you shouldn't do that. This seems like a bug. I will try to find it and let you know of the result.

  • User Avatar
    0
    bowlvan created

    If I enable UnitOfWork on Action, anyway to complete UOM in "Catch Exception"? So default UOM will not be trigger after Action done.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    No, because the code calls your controller action creates the UOW and it commits it. Can you send your project to <a href="mailto:[email protected]">[email protected]</a> ? Normally this should work as expected, I want to try it with your project ?