1

I have a Visual Studio solution that hosts Web Forms and MVC. I recently upgraded all the project files in the solution to MVC 5. This breaks all the postbacks for pages named "default.aspx" due to how the form tag's action attribute is handled in MVC 5 (default.aspx pages return an empty string for the action and the postbacks fail). See this article and this forum question.

My problem is that I have multiple MVC views that route to URLs that end in "/" or a word. For example "mysite.com/about/" or "mysite.com/about". I can't use the solution answered here because it affects the MVC routes as well (I tried it and the MVC views don't render). The only solution I found that works is to manually go into each default.aspx page and use this code:

protected void Page_Init(object sender, EventArgs e)
{
    ((HtmlForm)((MyMasterPage)Page.Master)FindControl("form1")).Action = "Default.aspx";
}

Note that I cannot set the action attribute in the master page because it will affect all non-default.aspx pages.

Without any more to go on, I now have two options:

  1. Go into each default.aspx page and manually set the action attribute.
  2. Create a parent page, manually set the action attribute and make each default.aspx page inherit from it.

These solutions are not ideal. I noticed that if I create a new ASP.NET Web Application project and use the Web Forms template, there is no issue with the default.aspx pages.

What else can I do to overcome this issue with the empty action attributes?

Community
  • 1
  • 1
Halcyon
  • 14,631
  • 17
  • 68
  • 99
  • ...wait, you're using MVC with WebForms events? .... – Simon Whitehead May 01 '14 at 22:39
  • a) Why can't you check to see if the Action is empty, and if empty set it to default, then it won't affect non-default actions... or b) use the Control Adapter in the solution you linked to, which also won't affect non-default pages because the Action won't be empty? – Erik Funkenbusch May 01 '14 at 22:45
  • I'm not using MVC with Web Forms events. Microsoft is promoting one ASP.NET and that means you can use Web Forms, MVC or both. In my case, I inherited an old Web Forms project and wanted to use MVC, so I added all the required things to use MVC in addition to the existing Web Forms. – Halcyon May 01 '14 at 22:56
  • Can you give a better example of how the project is structured? I.e what folders the Default.aspx are in and also your Mvc Routes? – Xenolightning May 02 '14 at 02:56
  • @Erik Funkenbusch I can't use the form adapter technique because it affects my MVC pages. I added it and the pages that have the format {controller}/{action} get routed to {controller}/{action}/default.aspx – Halcyon May 08 '14 at 15:38
  • @Xenolightning the default.aspx pages are nested at least one level deep from the root e.g. "~/projects/default.aspx". I'm using MVC views and controllers the same way I would in any normal MVC project (views and controllers are in "Views" and "Controllers" folders on the root and in also in areas). The route registration takes place in App_Start/RouteConfig.cs and in the area registration files. – Halcyon May 08 '14 at 15:42
  • It's impossible for a control adapter to affect MVC pages.. MVC pages don't use Webforms controls. – Erik Funkenbusch May 08 '14 at 16:26

1 Answers1

0

I know it's not what I wanted, but I ended up using the PreInit event handler in the master page to deal with the default.aspx pages.

protected void Page_PreInit(object sender, EventArgs e)
{
    try
    {
        var masterPage = Page.Master as MasterPages.Toolbox;

        if (masterPage != null)
        {
            var form = masterPage.FindControl("form1") as HtmlForm;

            if (form != null)
            {
                if (string.IsNullOrEmpty(form.Action))
                {
                    if (string.IsNullOrEmpty(Request.Url.Query))
                    {
                        form.Action = "default.aspx";
                    }
                    else
                    {
                        form.Action = "default.aspx" + Request.Url.Query;
                    }
                }
            }
        }
    }
    catch (Exception exception)
    {
        // Handle exception...
    }
}
Halcyon
  • 14,631
  • 17
  • 68
  • 99