html.beginform呈现的表单中的url无效

时间:2015-08-08 00:05:15

标签: asp.net asp.net-mvc routing asp.net-mvc-routing

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

HomeController是一个简单的控制器:

proc sql;
    create view Class_1 as 
    Select min(Name) as Name
         , min(Sex) as Sex
         , put(min(Age),best32.) as Age
         , put(min(Height),best32.) as Height
         , put(min(Weight),best32.) as Weight
    from sasHelp.Class
    Union
    Select max(Name) as Name
         , max(Sex) as Sex
         , put(max(Age),best32.) as Age
         , put(max(Height),best32.) as Height
         , put(max(Weight),best32.) as Weight
    from sasHelp.Class;
quit;
proc transpose data=Class_1
    out=Class(rename=(
        _name_ = Variable
        col1 = Minimum
        col2 = Maximum
    ));
    var Name Sex Age Height Weight;
run;

在视图 - public class HomeController : Controller { public ActionResult Index() { return View(); } } 上,我使用/Views/Home/Index.cshtml呈现表单。

Html.BeginForm

路由配置如下。前2个路由使用MapPageRoute映射到webforms。

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

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

  

/管理行动=索引&安培;控制器=家庭

不是“/ Home /”即使我已通过“索引”&amp; “Home”分别为Html.Beginform的actionname和controllername。

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

1 个答案:

答案 0 :(得分: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 }
);

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