简体   繁体   English

ASP.NET Core 2.1 RedirectToAction将TempData附加到url

[英]ASP.NET Core 2.1 RedirectToAction appends TempData to url

This is my save function. 这是我的保存功能。 It is under a controller called Repositories. 它在一个称为“存储库”的控制器下。 When I save a new Repository, it appends this to my 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 HTTPS://本地主机:44325 /库/库的TempData =%5BSuccess,%20Repository%20Created%5D&ViewBag = Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.DynamicViewData&的HttpContext = Microsoft.AspNetCore.Http.DefaultHttpContext&请求= Microsoft.AspNetCore.Http。 Internal.DefaultHttpRequest和响应= 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 =微软。 AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadataProvider&ModelBinderFactory = Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory&URL = Microsoft.AspNetCore.Mvc.Routing.UrlHelper&ObjectValidator = Microsoft.AspNetCore.Mvc.Internal.DefaultObjectValidator&用户= System.Security.Claims.ClaimsPrincipal

I understand that it has something to do with RedirectToAction("Index",this) as when I use Redirect("Index"), the url just ends with a /Index. 我知道这与RedirectToAction(“ Index”,this)有关,因为当我使用Redirect(“ Index”)时,URL刚以/ Index结尾。 However, I would like the Index word to be hidden and RedirectToAction does that. 但是,我希望索引字被隐藏,而RedirectToAction可以做到这一点。 How can I solve this issue? 我该如何解决这个问题?

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);
}

The problem lies in this return statement: 问题出在以下return语句中:

return RedirectToAction("Index", this);

According to RedirectToAction overloads list , the second parameter may contain routeValues while an object passed to it instead of string: 根据RedirectToAction重载列表 ,第二个参数可能包含传递给对象的routeValues而不是字符串:

public virtual RedirectToActionResult RedirectToAction (string actionName, object routeValues)

Hence, you're actually passing ControllerBase instance as routeValues parameter. 因此,您实际上是将ControllerBase实例作为routeValues参数传递。 Therefore, you should provide controller name instead: 因此,您应该改为提供控制器名称:

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

If you want to pass routeValues parameter together with controller name, use RedirectToAction with 3 overloads like this: 如果要与控制器名称一起传递routeValues参数,请使用RedirectToAction3个重载,如下所示:

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

Note: 注意:

The RedirectToAction uses HTTP GET method, which passed route parameters as query string, therefore a viewmodel object doesn't fit into that. RedirectToAction使用HTTP GET方法,该方法将路由参数作为查询字符串传递,因此viewmodel对象不适合该方法。 You should use another TempData or Session state instance to pass the viewmodel object to another controller. 您应该使用另一个TempDataSession状态实例将viewmodel对象传递给另一个控制器。

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

The main problem is already addressed in another answer. 另一个答案已经解决了主要问题。

I will address 我会解决

However, I would like the Index word to be hidden 但是,我希望索引词被隐藏

With attribute routing, you can set an empty route template 使用属性路由,您可以设置一个空的路由模板

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


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

    //...
}

That way when invoking return RedirectToAction("Index") , the generated URL will be what was configured in the route template. 这样,当调用return RedirectToAction("Index") ,生成的URL将是在路由模板中配置的URL。

Reference Routing to controller actions in ASP.NET Core 在ASP.NET Core中参考路由到控制器操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM