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:
- Go into each default.aspx page and manually set the action attribute.
- 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?