0
votes

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?

1

1 Answers

0
votes

I got it to work by following the instructions in this post:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapPageRoute(
    routeName: "admin",
    routeUrl: "{pagename}",
    physicalFile: "~/{pagename}.aspx");

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

However, I cannot explain why the parameter is necessary to make it not match in this case.