OnActionExecuting not executing in referenced MVC project

Viewed 86

I have multiple ASP.NET MVC 5 projects who have headers/menus at the top of the page where the html is exactly the same. There are some menu items, logos, links that get pulled from the database. To eliminate these redundant code across projects, I wanted to use a shared project for just the header, compile and then reference it in each application. I used RazorGenerator.Mvc so my view would be compiled in the DLL and I also use the MvcSiteMapprovider for the menu. So far everything works well. In the shared/referenced project I made a view called "CommonHeader.cshtml" and when I add this partial view to my other MVC projects and compile, it's all good.

I'm having some issue with Sessions, though. In the shared project's controller, I have the following function

protected override void OnActionExecuting

...which calls a sproc and loads some stuff like links/logos into TempData which is then shows in my header eg TempData.Peek("fieldname"). When I run the shared project itself, it loads these TempData fields I put into the View just fine. When I rebuild the other application projects that reference this project, these session/tempdata fields seem to be null. I'm not referencing the session variables from the consuming application views, but from the shared project itself so you would think it would work, but it appears that this OnActionExecuting function doesn't get executed when the code being referenced from another project.

Oddly enough the class that is used by my MvcSiteProvider to build the sitemap/menu will load stuff into Sessions just fine still. So I know I assume the problem ins't that a referenced project can't utilize Session. It just seems like the controller in the referenced project doesn't seem to be running the OnActionExecuting code. Does anyone know why? Or how I can get this to work without having to load all my Session into in that class that loads the SiteMap, keeping it inside the home controller?

1 Answers

I never did figure this out, but I think I came up with a better way to handle it. I can just create a Model that calls the stored procedure used to get all this information (logo, links, etc) and return this to my header view. Granted, it will get called every single time a page loads, but if it's only a few fields it's not a big deal.

Related