Pergunta

Background: I am working on an existing Webforms site which we are converting to MVC 4. We are also updating the UI so are making new MVC Layout pages to replace the old webforms master pages. We cannot upgrade all the aspx pages to mvc at once so in order to keep a consistent look to the site, the existing aspx that have not yet been converted are rendered inside the MVC layout. We are using MVC routing to send urls such as ~/pageName.aspx to an MVC Controller (with corresponding view) which uses Server.Execute to get the body of the aspx to render into the MVC layout. Up to this point it is working fine.

The Problem: In a couple of places in the existing aspx code behind there are some SessionState items being set. For some reason that I cannot uncover when the page is redirected (ie it goes through the MVC controller again) the sessionState is empty. What would cause this / where can I look to fix my problem? An example of where session variables are set is as follows.

.aspx

<asp:UpdatePanel ID="SignaturesUpdatePanel" runat="server">
    <ContentTemplate>
        <div>
            Signatures for your login are <asp:Literal ID="SignaturesStatusLiteral" runat="server" />.<br />
            <asp:LinkButton ID="SignaturesButton" Text="Turn signatures off temporarily" runat="server" OnClick="SignaturesButton_Click" />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

.aspx.cs

protected void SignaturesButton_Click(object sender, EventArgs e)
{
    var signaturesDisabled = Session["SignaturesDisabled"];
    bool disabled = signaturesDisabled == null || !bool.Parse(signaturesDisabled.ToString());
    Session["SignaturesDisabled"] = disabled;

    if (disabled)
    {
        SignaturesButton.Text = "Turn signatures on";
        SignaturesStatusLiteral.Text = "off";
    }
    else
    {
        SignaturesButton.Text = "Turn signatures off temporarily";
        SignaturesStatusLiteral.Text = "on";
    }
}

Obviously prior to my work with using the MVC controller this all works fine (ie when it is all just running as the original WebForms app with no MVC involvement). Also, as a test I tried setting a session variable in the controller action method and then retrieving it in the .aspx.cs file which worked fine so I imagine there must be something that clears the session when it is being routed through MVC.

for completeness here is the relevant route mapping

routes.MapRoute(
    name: "Project File Pages",
    url: "{page}.aspx",
    defaults: new { controller = "Legacy", action = "ProjectFileLayout" }, 
    constraints: new { file = new QueryStringConstraint() }
);

and (trimmed down) MVC controller action method

public ActionResult ProjectFileLayout(int file)
{
    var url = GetCurrentPageUrl();
    var sw = new StringWriter();
    Server.Execute(url, sw, true);

    ...

    var model = new LegacyViewModel
    {
        Body = sw.ToString(),
        ...
    };
    return View(model);
}

If you need more details or I have not been clear enough in my question, please let me know!

Foi útil?

Solução

Through investigating another problem I was having, I have now discovered that it is the UpdatePanel to blame. The page is reloaded because of the updatePanel and because of the routing, the MVC controller action picks it up. this then causes the whole page to be re-rendered rather than just the updatePanel as would have happened in native webforms.

to fix I am going to make another route mapping to collect things that have the corresponding header details and then just send the updatePanel string to be rendered.... hopefully should work wonders for both my problems!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top