2014-09-26 153 views
0

我有一个表格来管理的意见插入:重定向后登录无法工作

@model GatorsWebSite.Comment 
    @using (Html.BeginForm("Create", "Comments", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(m => m.ArticleID) 
    @Html.TextAreaFor(m => m.Text, new { @class = "form-control wide", @placeholder = "Type your comment", @rows = "3" }) 
    @Html.ValidationMessageFor(m => m.Text, "", new { @class = "text-danger" }) 
    } 

这是控制器的操作:

[Authorize] 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "Text, ArticleID")] Comment comment){ 
    if (comment == null || comment.ArticleID <= 0) 
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 

    if (ModelState.IsValid){ 
    comment.Date = DateTime.Now; 
    comment.UserID = User.Identity.GetUserId(); 
    _commentRepository.Add(comment); 
    _commentRepository.Save(); 
    } 
    return RedirectToAction("Details", "News", new { ID = comment.ArticleID }); 
} 

由于正在采取行动的授权,如果用户没有在

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl){ 
     if (ModelState.IsValid){ 
     var user = await UserManager.FindAsync(model.UserName, model.Password); 
     if (user != null){ 
      await SignInAsync(user, model.RememberMe); 
      return RedirectToLocal(returnUrl); 
     } 
     else{ 
      ModelState.AddModelError("", "Invalid username or password."); 
     } 
     } 
     return View(model); 
    } 

enter image description here

记录

enter image description here

重定向URL将被评论/创建和将失败,一个404错误,确实存在管理这个问题的通用解决方案,因为我不能用一个简单的重定向操作?

+0

工作时,您所提交的表单目标行动是不是调用它给404没有找到? – 2014-09-26 11:04:46

+0

@Kartikeya我已经更新了错误 – InferOn 2014-09-26 11:17:20

+0

而不是'return RedirectToLocal(returnUrl);'try'return RedirectToAction(“Create”,“Comments”);' – 2014-09-26 11:21:54

回答

1

一种替代方法是让Create获得操作并将其重定向到文章/新闻列表页面。例如:

public ActionResult Create(){ 
    return RedirectToAction("Index", "News"); 
} 

编辑

和正是我做了什么:

[Authorize] 
    [HttpGet] 
    public ActionResult Create([Bind(Include = "Text, ArticleID")] Comment comment) 
    { 
     if (comment == null || comment.ArticleID <= 0) 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 

     if (ModelState.IsValid) 
     { 
     comment.Date = DateTime.Now; 
     comment.UserID = User.Identity.GetUserId(); 
     _commentRepository.Add(comment); 
     _commentRepository.Save(); 

     } 

     return RedirectToAction("Details", "News", new { ID = comment.ArticleID }); 
    } 

...并像一个魅力