C# 重定向路由不';行不通
我正在制作我的第一个网络应用。到目前为止,我对redirectToRoute或redirectToAction没有任何问题。在post操作结束时,我想重定向,但根本不起作用。这是我的代码:C# 重定向路由不';行不通,c#,asp.net-mvc,C#,Asp.net Mvc,我正在制作我的第一个网络应用。到目前为止,我对redirectToRoute或redirectToAction没有任何问题。在post操作结束时,我想重定向,但根本不起作用。这是我的代码: [HttpPost] public ActionResult Delete(int id) { var post = this.Data .Posts .All() .Where(x => x.
[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" });
}
这是康斯特
public static readonly string RedirectToRouteShowThread = "Show thread";
我的意思是它不起作用,因为它没有重定向其他一切都按预期工作。什么是
CommonConstants.RedirectToRouteShowThread
返回?它是否与路由名称匹配?同时发布操作方法的签名。说“不起作用”没有帮助。这是一个完全无用的问题陈述,没有给出任何实际原因或错误的暗示。如果你想在这里提问,你需要学习如何提出有用的问题。RedirectToRoute会向浏览器发送带有新URL的302。您应该能够在浏览器地址栏中看到这一点。使用您喜欢的任何浏览器调试工具,例如Fiddler、IE开发工具、Google开发工具,查看返回到浏览器的内容。