2017-08-10 80 views
0
不工作

我在RouteConfig.cs其下方顯示定義的默認路由:發佈形式不同的控制器行動MVC

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

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

後端控制器:

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

帳戶控制器:

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

     [HttpPost] 
     [ValidateAntiForgeryToken()] 
     public ActionResult LoginForm(Login item) 
     { 
      //Some code goes here... 
      return RedirectToAction("Index"); 
     } 
    } 

登錄部分:

using (Html.BeginForm("LoginForm", "Account", FormMethod.Post)) 
    { 
     @Html.AntiForgeryToken() 

     @Html.LabelFor(m => m.Username) 
     @Html.TextBoxFor(m => m.Username, new { placeholder = "Username" }) 
     @Html.ValidationMessageFor(m => m.Username) 

     @Html.LabelFor(m => m.Password) 
     @Html.PasswordFor(m => m.Password, new { placeholder = "Password" }) 
     @Html.ValidationMessageFor(m => m.Password) 

     <input type="submit" value="Login" /> 
    } 

我的形式不提交到控制器動作Account/Login

+1

你的路由定義必須是'網址:「{控制器}/{行動}/{ID}」,' –

+0

@StephenMuecke我不想定義上的網址控制器有任何其他方式來實現這個? –

+2

那麼你如何期待區分一個控制器? –

回答

0

然後,你將不得不添加另一條路線處理該url。然而,這違背了基於約定的路由的目的。

routes.MapRoute(
    name: "Login", 
    url: "Login", 
    defaults: new { controller = "Account", action = "LoginForm" } 
); 

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