0

I've been asked by our architect to find a way to capture state information on an MVC page that cannot normaly captured using @model. At present, I'm using a combination of Ajax to send the information off to a controller, which then takes it and shoves it into a Session variable to be picked up as needed. This solution is meant to be part of a framework that can be plugged into any future projects.

The problem is, the Ajax part of it is generic; it can be shoved into any MVC page and will do its job. Unfortunately, the controller part is not so generic and would need to be defined for each and every project.

So, to my question: is there any way to populate a session variable (with or without ajax) directly from the calling page. It does not need to be a session variable either, it can be TempData or even a static variable, as long as the solution can be dropped into project X and pick up the information as needed.

Nor am I against use of the controller, if there is a way to plug one generically into a project. So, while my question is about Ajax and Session variables, it's open to any solution that allows me to push the current form state someplace where it can be used with minimal modifications to Project X.

Any and all suggestions welcome. :)

1 Answer 1

1

You can define a BaseController which all other controllers in the MVC project will derive from. The BaseController will have a ActionFilter to capture state information to be accessible on an MVC pages in the project. This ActionFilter will be executed before every ActionResult method.

[Authorize]
public class BaseController : Controller
{
   protected override void OnActionExecuting(ActionExecutingContext filterContext)
   {
      if (filterContext.HttpContext.Session != null)
      {
         // You can even have any custom logic in here

         string userName = HttpContext.User.Identity.Name;
         if (Session["logonUser"] == null)
         {
              Session["logonUser"] = userName.Length > 1 ? userName.ToUpper() : "INVALID USER";
         }
      }
   }
}

And then in you respective MVC page you can derive the BaseController like

 public class ReportsController : BaseController
 {
   .....
   .....
 }
Sign up to request clarification or add additional context in comments.

7 Comments

Beat me to it, I was typing up pretty much the exact same thing. +1
As an alternative, (if you still want to use the AJAX...say the data you need is generated on-page) place the Action that the AJAX function calls in this base controller, and call it from there. Then stick the AJAX function in the Master page.
@guildsbounty I agree.
While this is a good idea and I had considered it, what worried me was simply the URL call in the Ajax method. In this example, I'd have to specify the URL as /Reports/AjaxCall, but I don't know what it will be from project to project. I did try, on a whim, /Base/AjaxCall, but that didn't work either. The OnActionExecuting does fire even if the URL doesn't exist, but it only passes the data I needed when the URL was valid. Or am I missing something here?
Hmmm... HandleUknownAction looks like it could be used to solve the unknown controller issue if I give it a unique and unknown name... but it only receives "ActionName." I note that I can interrogate the Request object there, but does that include information passed by the data parameter? Or is there another object for that? I'm assuming no...
|

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.