In ASP.NET MVC, I want to set the controller properties from ActionFilter.

Asked 1 years ago, Updated 1 years ago, 47 views

Thank you for your help.

ASP.NET MVC has multiple controllers that inherit a common basic class, all configured with the same ActionFilter.

public class BaseController:controller
{
    public MemberParam mParam;
}

AnalysisSession
public class memberController:BaseController
{
    // some codes
}

AnalysisSession
public class HomeController:BaseController
{
    // some codes
}

Inside the AnalysisSession action filter, I would like to set a value for the internal properties of the called controller.

public class AnalysisSessionAttribute:ActionFilterAttribute
{
    public override void OnActionExecuting (ActionExecutingContext context) {
        var sessions = context.HttpContext.Session;
        // Get session(s) and get(s)
        // a part of something that is not understood here
        var controller=context.Controller as MemberController;
        controller.mParam = this.mParam;
    }
}

With a single controller, as you can see, it's a problematic reference, but with multiple controller/many properties, please tell me how to implement it simply.

c# asp.net

2022-09-29 21:47

1 Answers

Based on the above comments, I will reply as self-responsive.

public class AnalysisSessionAttribute:ActionFilterAttribute
{
    public override void OnActionExecuting (ActionExecutingContext context) {
    var sessions = context.HttpContext.Session;
    // Get session(s) and get(s)
    // use a polymorphism to cast into the inheritance class
    var controller=context.Controller as BaseController;
    controller.mParam = this.mParam;
}


2022-09-29 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.