Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

ActionExecutingContext result response return

public override void OnActionExecuting(HttpActionContext actionContext)    
{ 
    actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
Comment

ActionExecutingContext result response return

public override void OnActionExecuting(ActionExecutingContext context)
{
  context.Result = new UnauthorizedObjectResult("user is unauthorized");
}
Comment

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 { }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# recursion formula for the factorial 
Csharp :: rotate along normal unity 
Csharp :: c# get list object type of generic list 
Csharp :: how to turn on/off Particle System unity 
Csharp :: recursively reverse linked list 
Csharp :: Allow edit in Datagrid C# 
Csharp :: defining vectors in c# 
Csharp :: set the page that FormsAuthentication.RedirectFromLoginPage redirects to 
Csharp :: c# list find index 
Csharp :: vb.net check if datatable has rows 
Csharp :: remove header visual studio android 
Csharp :: C# max rand 
Csharp :: how to fix on GetMouseButtonDown activating UI unity 
Csharp :: exceeds your upload_max_filesize ini directive (limit is 2048 KiB). 
Csharp :: max index array c# 
Csharp :: wpf get dynamic resource from code 
Csharp :: take photo function in unity 
Csharp :: list of function in c# 
Csharp :: foreach c# linq example 
Csharp :: context.Response.Body read stream .net 
Csharp :: nunit cleanup after all tests 
Csharp :: What are logic gates? 
Csharp :: unity draw waypoints path 
Csharp :: spin with rigidbody 2d unity 
Csharp :: irrrtate throught an matrix c# 
Csharp :: Getting the text from a drop-down box 
Csharp :: c# lambda group by multiple columns 
Csharp :: or operator in shell 
Csharp :: check if object has parent unity 
Csharp :: wpf listbox binding change style of selected item 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =