我有一个简单的登录表单,我希望发布到ASP.Net MVC。我发现如果操作设置为/ Login它可以工作。但如果我写出动作名称,如“/ Login / Index”,那么它就会失败。服务器将302返回到登录页面,并且永远不会调用Index()的POST版本。这是为什么?
这是HTML:
<form action="/Login/Index/" method="post">
<label for="username">Username</label>
<input type="text" id="username" name="username" maxlength="60" />
<label for="password">Password</label>
<input type="password" id="password" name="password" maxlength="20" />
<input type="submit" value="Login" />
</form>
这是控制器:
public class LoginController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string username, string password)
{
return null;
}
}
这是Global.asax中定义的唯一路径
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
解决: 在我的Web.Config中,我有loginUrl =“〜/ Login”,它需要是“〜/ Login / Index”,否则URL被拒绝访问,并重定向回登录页面。
答案 0 :(得分:1)
在我的Web.Config中,我有loginUrl="~/Login"
,需要"~/Login/Index"
。由于匿名访问被拒绝,因此拒绝指定为loginURL
的URL之外的任何URL,并重定向回登录页面。
答案 1 :(得分:0)
“/ Login / Index”和“/ Login / Index /”是不同的操作。如果你取出尾随/,它可能会按你期望的方式工作。
答案 2 :(得分:0)
尝试将[HttpGet]添加到第一个方法