Switching to {controller}/{id}/{action} breaks RedirectToAction

后端 未结 6 1948
后悔当初
后悔当初 2021-01-04 23:55

I am trying to use proper REST urls with MVC. To do that I switched default Routing from:

{controller}/{action}/{id}
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 00:46

    I was having a similar problem. Route values that were passed to my controller action were being reused when I tried to redirect the user with RedirectToAction, even if I didn't specify them in the new RouteValueDictionary. The solution that I came up with (after reading counsellorben's post) with was to clear out the RouteData for the current request. That way, I could stop MVC from merging route values that I didn't specify.

    So, in your situation maybe you could do something like this:

    [CustomAuthorize]
    [HttpGet]
    public ActionResult Approve(int id)
    {
        _customerService.Approve(id);
        this.RouteData.Values.Clear();  //clear out current route values
        return RedirectToAction("Search");  //Goes to bad url
    }
    

提交回复
热议问题