首页 文章

html.beginform呈现的表单中的网址无效

提问于
浏览
1

我有一个WebForms和MVC混合的应用程序 .

HomeController是一个简单的控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

在视图 - /Views/Home/Index.cshtml 上,我使用了 Html.BeginForm 来渲染表单 .

@using(Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal"}))
{
    @Html.TextBox("username", "");
    @Html.Password("password", "");
    <button type="submit">Login</button>
}

路由配置如下 . 前两个路由使用MapPageRoute映射到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 }
 );

但是在我的主页(“Home”是我的默认控制器)呈现给浏览器之后,表单操作有了url:

/ admin?action = Index&controller = Home

不是“/ Home /”,即使我已经将“Index”和“Home”分别作为actionname和controllername传递给Html.Beginform .

当我浏览我的主页时,网址路由工作正常 . 但表单的“动作”具有无效的URL . 它似乎匹配我配置的第一条路线 . 为什么?

1 回答

  • 0

    我按照说明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 }
    );
    

    但是,我无法解释为什么在这种情况下必须使参数不匹配 .

相关问题