1

I have a controller like this:

public class LoginController : Controller
{
    private Int32 _companyID;

    // My actions
}

This variable _companyID should (obviously) contain the id of the company, and this value is currently on the session. I want to get that value and set on the controller variable.

Edit: I'm using this variable because I have to check some things about this value from the session and I don't want to duplicate this code in every action. Instead, I just want to check what I need, set this value in the variable and use it inside my actions.

On WebForms, I'd simply get the value from the session in the page load event and it'd be done. But in MVC, I don't know how I do that.

My first thought was get this value in the constructor, something like that:

public LoginController()
{
    _companyID = Convert.toInt32(Session["companyID"]);
}

But I discovered that session can't be accessed on constructors (it was null for me).

Then I thought about using filters, but I wasn't able to figure out how I set the value in the controller variable. I've tried in this way:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
    Int32 _companyID = Convert.toInt32(HttpContext.Current.Session["idEmpresa"]);

    // I couldn't pass this value directly to the controller variable
}

I've seen a few ways to pass values from filters to controllers, but all of them imply in extra code in my actions to get that value from the filter, and I don't want that. I'd like a way that I can get the desired value without change every action that I have or will have.

Is there a way to get that?

I hope that my question is clear enough. Thanks in advance!

1 Answer 1

1

First, the variable is not global, it's an instance variable on the controller. A bit pedantic, yes, but those are entirely different things.

Second, there's no point to setting an ivar on the controller with the value from the session, anyways, because the controller is instantiated and disposed with each request. In other words, this is never shared between actions, regardless; each time you call a new action, it has to re-set the ivar.

Finally, while you could use a filter, it's unnecessary because of the last point. If you need the session variable in your controller action, just access it directly from that action. If you need to pass it to the view, you can set it in ViewBag or on a view model.

Sign up to request clarification or add additional context in comments.

6 Comments

I understand your point, but I have to check some things about this value from the session (I forgot to mention that in my question, sorry), and I don't want to duplicate this code on every action. I want to separate this code somewhere and just use the value in the controller variable in my actions. Can I do this?
Then factor out the code into a private method on the controller and call that private method in each action.
I've thought that there would be a way to do this without changing all my actions. So there isn't?
You could go ahead and create that custom filter, or override the controller's OnActionExecuting. Just don't worry about setting that ivar. Just do whatever work needs to be done there.
But is there a way to access controller variables from filters? Sorry for insisting, but it might be helpful someday, and I didn't see anything about it in my researches.. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.