I have an application which is mix of WebForms and MVC.
HomeController is a simple controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
On view - /Views/Home/Index.cshtml
, I have used Html.BeginForm
to render the form.
@using(Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal"}))
{
@Html.TextBox("username", "");
@Html.Password("password", "");
<button type="submit">Login</button>
}
Routes are configured like below. First 2 routes are using MapPageRoute to map to webforms.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("admin", "admin", "~/admin.aspx");
routes.MapPageRoute("adminEdit", "adminEdit", "~/adminEdit.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But after my homepage ("Home" is my default controller) is rendered to the browser, form action has url:
/admin?action=Index&controller=Home
not "/Home/" even though i have passed "Index" & "Home" as actionname and controllername to Html.Beginform respectively.
Url routing is working fine when i browse to my homepage. But the form's "action" has invalid url. It seems like it is matching the first route i have configured. Why?