Horje
ActionExecutingContext result response return objects Code Example
ActionExecutingContext result response return
public override void OnActionExecuting(HttpActionContext actionContext)    
{ 
    actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
ActionExecutingContext result response return objects
public class Student
{
  public string Name { get; set; }
}

 [ApiController]
    public class TestController : ControllerBase
    {
        [HttpGet, Route("api/Test/GetString")]
        [SampleActionFilter]
        public ActionResult<Student> GetString(string name)
        {
            if(name.StartsWith("s"))
            {
                return Ok(new Student{ Name = $"This is data {name}" });
            }
            else
            {
                return Ok(new Student { Name = $"No Name" });
            }
        }
    }

 public class SampleActionFilterAttribute : TypeFilterAttribute
    {
        public SampleActionFilterAttribute() : 
        base(typeof(SampleActionFilterImpl))
        {
        }

    private class SampleActionFilterImpl : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
        
            // perform some business logic work

        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            // perform some business logic work
            var myResult = (OkObjectResult)context.Result;

            //Add type checking here... sample code only
            //Modiy object values
            try
            {
                Student myVal = (Student)myResult.Value;
                myVal.Name = "Johnny";
            }
            catch { }
        }
    }
}
ActionExecutingContext result response return
public override void OnActionExecuting(ActionExecutingContext context)
{
  context.Result = new UnauthorizedObjectResult("user is unauthorized");
}




Csharp

Related
get path revit link unloaded Code Example get path revit link unloaded Code Example
Coroutine after another Code Example Coroutine after another Code Example
aps.net core mvc chek box Code Example aps.net core mvc chek box Code Example
only specific columns in Linq Code Example only specific columns in Linq Code Example
visual studio console.writeline not showing in output window Code Example visual studio console.writeline not showing in output window Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7