【发布时间】:2018-12-11 01:11:00
【问题描述】:
这是我的保存功能。它位于一个名为 Repositories 的控制器下。当我保存一个新的存储库时,它会将其附加到我的 url:
我知道它与 RedirectToAction("Index",this) 有关,因为当我使用 Redirect("Index") 时,url 只是以 /Index 结尾。但是,我希望索引词被隐藏,而 RedirectToAction 会这样做。我该如何解决这个问题?
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(CreateRepositoryViewModel Input)
{
if (!ModelState.IsValid)
{
return View("Create", Input);
}
var directory = directoriesController.Create(Input);
if (directory != null)
{
Input.Path = directory;
var result = repositoriesData.Save(Input);
}
else
{
TempData["Error"] = "Repository Creation" + LoggingGlobals.Error;
return RedirectToAction("Index", this);
}
TempData["Success"] = "Repository " + LoggingGlobals.Create;
return RedirectToAction("Index", this);
}
【问题讨论】:
-
您的控制器名称是什么?您是否尝试将
TempData从Save方法传递给Index方法? -
URL 结构看起来很可疑,通常应该显示
https://localhost:44325/Repositories/Index、https://localhost:44325/Repositories或类似的内容。我认为TempData内容不应作为查询字符串的一部分传递。 -
不!问题在于
this关键字!我有这个问题的答案,但我正在等待提问者的评论。 -
return RedirectToAction("Index", this);=> 第二个参数是routeValues,在查询字符串中表示。传递this包含ControllerBase实例,其中包含模型状态和TempData。 -
我建议不要让这里的每个人都感到困惑,而是通过教程(或 2 个)更快地学习一些基础知识:docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
标签: c# asp.net asp.net-mvc asp.net-core