1

I have a base web ui project, I also have other 'plugin' projects which are just other mvc web applications.

I take the dll's and views from them and toss them into my main web ui's bin and views folder so they can just be added or removed at any time which does work.

each 'plugin' contains a GET method that is called from the main web ui to load the menu options from each of the 'plugins'.

After each URL is called, the main web ui fires the `Session_Start'

menu.Append(HelperMethods.GetModuleMenuHTML(controller, SecurityController.CurrentDomain()));

public static string GetModuleMenuHTML(string controllerName, string currentDomain)
    {
        string html = string.Empty;
        try
        {
            //THIS LINE HERE IS CAUSING Session_Start to fire again
            //IN THE MAIN WEB UI
            html = new WebClient().DownloadString(string.Format("{0}/{1}/GetMenu", currentDomain, controllerName));
        }
        catch (Exception ex)
        {

        }
        return html;
    }

What is causing Session_Start to fire when calling html = new WebClient().DownloadString(string.Format("{0}/{1}/GetMenu", currentDomain, controllerName)); and how can I prevent that from happening?

Does this have something to do with me just dropping in the dll and views from another project directly into the main web ui projects as a "plugin" (which does however load correctly into my UI) to be easily added and removed?

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Tsukasa
  • 6,342
  • 16
  • 64
  • 96

2 Answers2

1

Its mainly because of this:

html = new WebClient().DownloadString(string.Format("{0}/{1}/GetMenu", currentDomain, controllerName));

This line uses WebClient class to get the html, but the WebClient class is stateless, and each time its called it uses another request with no cookies, so the server thinks it a new request, and starts a new session.

MoustafaS
  • 1,991
  • 11
  • 20
  • What would be the best way to make this call then? – Tsukasa Jul 08 '16 at 22:54
  • It actually depends on a lot of things, like is the forms sending the reqeusts in the same domain as the asp.net app ? then the best would bhe to call directly and not use it as you do now, you have also the option of forwarding all the cookies coming with the WebClient request, but that would be rather bad method – MoustafaS Jul 08 '16 at 22:56
  • The other 'plugins' have no reference to the main web ui. The webui is company.webui the plugins are company.module.nameofmodule – Tsukasa Jul 08 '16 at 23:00
  • 1
    @Tsukasa You can use this as a more clean way http://stackoverflow.com/q/483091/1419970 – MoustafaS Jul 08 '16 at 23:10
  • 1
    Thanks I switched to using RenderRazorView. – Tsukasa Jul 10 '16 at 21:52
1

The WebClient request is recursively starting a new session. As a hack, you could modify your Session_Start() to check if the incoming url is /{controller}/GetMenu and simply avoid the WebClient call. See: https://stackoverflow.com/a/18656561.

Otherwise, perhaps decorating your MenuController with SessionStateAttribute (https://msdn.microsoft.com/en-us/library/system.web.mvc.sessionstateattribute(v=vs.118).aspx) might avoid the Session_Start altogether (if GetMenu() doesn't use the session state).

Community
  • 1
  • 1
Mark Glasgow
  • 529
  • 2
  • 9
  • Perfect using `[SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]` in the 'plugins' works. With what i'm doing here, is it a proper way to implement something like this or is it bad practice since there are so many ways to do a task. – Tsukasa Jul 09 '16 at 00:50
  • I agree with MoustafaS below. I would try and use RenderViewToString() (as implemented here http://stackoverflow.com/a/2759898/154355). This would avoid the http request and improve performance. As GetMenu() doesn't use any session state, it might also be possible to render it once in Application_Start and use the cached version in Session_Start? – Mark Glasgow Jul 10 '16 at 01:49
  • The main web ui is unaware of the other controllers that contain GetMenu so I would need to pass the controller name as a string as well as the action GetMenu. Is there a way to modify it to do so? – Tsukasa Jul 10 '16 at 02:49