returnUrl没有传递给AccountController中的LogOn操作方法

时间:2012-06-21 00:03:12

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

我有一个从Internet模板创建的MVC应用程序以使用表单身份验证,当用户登录时我想将它们指向例如Home | About页面。

LogOn方法是由项目模板创建的,无需修改;

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
           // etc, etc
        }

        return View(model);
    }

我在Web.config中设置了defaultUrl,如下所示;

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" defaultUrl="~/Home/About"/>
</authentication>

但是,当调用HomeController.LogOn时,returnUrl参数始终为null。请注意LogOn方法上的HttpPost属性,因此无法在查询字符串中传递url。

如何配置返回网址,以便将其传递给LogOn操作方法,并在登录后将用户定向到网址?

1 个答案:

答案 0 :(得分:1)

最简单的方法是将Global.asax中的默认路由更改为/Home/About而不是/Home/Index

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "About", id = UrlParameter.Optional } // Parameter defaults
    );
}
相关问题