【问题标题】:ASP.NET Core 2.1 RedirectToAction appends TempData to urlASP.NET Core 2.1 RedirectToAction 将 TempData 附加到 url
【发布时间】:2018-12-11 01:11:00
【问题描述】:

这是我的保存功能。它位于一个名为 Repositories 的控制器下。当我保存一个新的存储库时,它会将其附加到我的 url:

https://localhost:44325/Repositories/Repositories?TempData=%5BSuccess,%20Repository%20Created%5D&ViewBag=Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.DynamicViewData&HttpContext=Microsoft.AspNetCore.Http.DefaultHttpContext&Request=Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest&Response=Microsoft.AspNetCore.Http.Internal.DefaultHttpResponse&RouteData=Microsoft.AspNetCore.Routing.RouteData&ModelState=%5BName,%20Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary%2BModelStateNode%5D&ControllerContext=Microsoft.AspNetCore.Mvc.ControllerContext&MetadataProvider=Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider&ModelBinderFactory=Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory&Url=Microsoft.AspNetCore.Mvc.Routing.UrlHelper&ObjectValidator=Microsoft.AspNetCore.Mvc.Internal.DefaultObjectValidator&User=System.Security.Claims.ClaimsPrincipal

我知道它与 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);
}

【问题讨论】:

  • 您的控制器名称是什么?您是否尝试将 TempDataSave 方法传递给 Index 方法?
  • URL 结构看起来很可疑,通常应该显示https://localhost:44325/Repositories/Indexhttps://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


【解决方案1】:

问题出在这个return语句上:

return RedirectToAction("Index", this);

根据RedirectToAction overloads list,第二个参数可能包含routeValues,而传递给它的是一个对象而不是字符串:

public virtual RedirectToActionResult RedirectToAction (string actionName, object routeValues)

因此,您实际上将 ControllerBase 实例作为 routeValues 参数传递。因此,您应该提供控制器名称:

return RedirectToAction("Index", "Repositories");

如果您想将routeValues 参数与控制器名称一起传递,请使用RedirectToAction3 overloads,如下所示:

return RedirectToAction("Index", "Repositories", new { parameterName = "value" });

注意:

RedirectToAction 使用 HTTP GET 方法,该方法将路由参数作为查询字符串传递,因此视图模型对象不适合该方法。您应该使用另一个 TempDataSession 状态实例将 viewmodel 对象传递给另一个控制器。

TempData["ViewModel"] = Input;
return RedirectToAction("Index", "Repositories");

【讨论】:

  • TempData["Error"] 只需要一个字符串
  • 您可以在TempData 中存储任何具有各种键名的对象,而不仅仅是一个字符串。如果您想将其作为查询字符串传递,请遵循 RedirectToAction 的 3 重载版本。
【解决方案2】:

主要问题已在另一个答案中解决。

我会解决的

但是,我希望隐藏索引词

使用属性路由,可以设置一个空的路由模板

[Route("[controller]")]
public class RepositoriesController {


    [HttpGet]
    [Route("")] //GET Repositories
    [Route("[action]")] //GET Repositories/Index
    public IActionResult Index() {
        return View();
    }

    //...
}

这样调用return RedirectToAction("Index")时,生成的URL就是路由模板中配置的。

参考Routing to controller actions in ASP.NET Core

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多