我正在制作我的第一个网络应用。到目前为止,我没有遇到redirectToRoute或redirectToAction的任何问题。在帖子操作结束时,我想重定向,但它根本不起作用,这是我的代码:
[HttpPost]
public ActionResult Delete(int id)
{
var post = this.Data
.Posts
.All()
.Where(x => x.Id == id)
.FirstOrDefault();
if (post != null)
{
var thread = this.Data
.Threads
.All()
.Where(x => x.Id == post.ThreadId)
.FirstOrDefault();
this.Data
.Posts
.Delete(post);
this.Data.SaveChanges();
return this.RedirectToRoute(CommonConstants.RedirectToRouteShowThread, new
{
area = "",
id = thread.Id,
title = thread.SubCategory.Title,
name = thread.Title,
action = "Display"
});
//return this.RedirectToRoute(CommonConstants.RedirectToRouteShowAllThreadsInSubCategory, new { Area = "", title = thread.SubCategory.Title });
// return this.Redirect("red/red");
}
return this.View();
}
我也尝试了不同的重定向测试,但仍然无效。如果重要,我会从js调用此动作。我也检查了它是否进入内部如果有条件,一切似乎都很好。我的错误在哪里?
编辑:这是我的路线
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Show thread",
url: "SubCategory/{title}/{action}/{id}-{name}/{page}",
defaults: new { controller = "Post", action = "Display", page = UrlParameter.Optional },
namespaces: new[] { "ForumSystem.Web.Controllers" });
routes.MapRoute(
name: "Create and display threads",
url: "SubCategory/{title}/{action}/{page}",
defaults: new { controller = "SubCategory", page = UrlParameter.Optional },
namespaces: new[] { "ForumSystem.Web.Controllers" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "ForumSystem.Web.Controllers" });
}
这是const
public static readonly string RedirectToRouteShowThread = "Show thread";
我的意思是它不起作用是它没有重定向所有其他工作按预期工作。