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 :: vb.net delete line from text file 
Csharp :: fluent api c# hasmany withmany 
Csharp :: c# convert address to int 
Csharp :: mpeg get video size 
Csharp :: finding holydays asp.net 
Csharp :: external font family uwp c# 
Csharp :: c# loop datatable column names convert to list 
Csharp :: How to execute a script after the c# function executed 
Csharp :: paging thru result from mongodb in C# 
Csharp :: Filter list contents with predicate (anonymous method) 
Csharp :: where are the viwes in .net core publish 
Csharp :: rename join table in many to many 
Csharp :: Delegate no parameter no return 
Csharp :: streamwriter delete all text 
Csharp :: list.SkipWhile in c# 
Csharp :: c# e-mail send 
Csharp :: search list for words c# 
Csharp :: Unity FPS camera z axis rotating problem 
Csharp :: get innermost exception c# 
Csharp :: disable quickedit c# 
Csharp :: C# Create Swiss QR-Bill API 
Csharp :: global variable startup file .net core api 
Csharp :: two question marks together mean in C# 
Csharp :: cshtml page title 
Csharp :: unity sprite blurry when far 
Csharp :: check for held hey unity 
Csharp :: .net core api routing not working 
Csharp :: version string c# 
Csharp :: custom convert list object to other object c# 
Csharp :: how to get variable value in properties file in inspector unity 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =