Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

.net return context.Result without extra new objectResult

//handles only exceptions caused by dividing by zero
public class DivideByZeroExceptionFilterAttribute : Attribute, IExceptionFilter
{
    public void OnException(ExceptionContext context)
    {
        //chech if this is divide by zero exception
        if (!(context.Exception is DivideByZeroException))
            return;

        var myerror = new
        {
            result = false,
            message = "Division by zero went wrong"
        };
        context.Result = new ObjectResult(myerror)
        {
            StatusCode = 500
        };

        //set "handled" to true since exception is already property handled
        //and there is no need to run other filters
        context.ExceptionHandled = true;
    }
}
Comment

.net return context.Result without extra new objectResult

public class ErrorModel
{
    public string Error { get; set; }

    public int Id { get; set; }

    public List<int> Values { get; set; }
}

//filter

var error = new ErrorModel
{
    Error = context.Exception.Message,
    Id = 1,
    Values = new List<int> { 1, 2, 3 }
};
context.Result = new ObjectResult(error)
{
    StatusCode = 500
};
Comment

PREVIOUS NEXT
Code Example
Csharp :: properties vs field c# 
Csharp :: c# boolean 
Csharp :: linq conditionnally add where clause 
Csharp :: An unhandled exception occurred during the execution of the current web request 
Csharp :: How to read key from web config in JavaScript 
Csharp :: create entity c# d365 
Csharp :: auto refresh gridview c# 
Csharp :: blazor OnInitializedAsync Unhandled exception rendering component: Cannot wait on monitors on this runtime. 
Csharp :: how to full screen login form using C# MVC 
Csharp :: imagetarget found event vuforia c# 
Csharp :: cqrs design pattern .net core 
Csharp :: unity c# store gameobject in array 
Csharp :: ASP.NET Core set update clear cache from IMemoryCache (set by Set method of CacheExtensions class) 
Csharp :: Transparent UserControl 
Csharp :: C# sprint key 
Csharp :: WixSharp-FirewallException 
Csharp :: Avoid auto-filling persian time picker 
Csharp :: unity control physics of multiple simulation 
Csharp :: c# check word length 
Csharp :: unity recttransform set anchor 
Csharp :: c# quaternion eular calculator 
Csharp :: dotnet DB context register 
Csharp :: prime number generator 
Csharp :: .net 6 get appsettings value 
Csharp :: swagger skip endpoint .net core 
Csharp :: c# second last element 
Csharp :: C# Bitwise and Bit Shift operator 
Csharp :: embed video to exe file with c# 
Csharp :: git change remote origin 
Html :: p5 cdn 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =