RedirectToAction redirects request parameters?

I am working on an ASP.NET MVC application and ran into something strange.

I got two controller actions:

[CustomAuthorize(Roles = SiteRoles.Admin)] public ActionResult Review(int? id) [CustomAuthorize(Roles = SiteRoles.Admin)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Review(AdReview data) 

First, I call the Review action with a null parameter, this will create a web page with a list of elements. Elements are associated with the first Review action with an identifier set.

When an identifier has been provided for verification, an edit web page will be returned for this element. After you click, after some changes we will finish the second action (message). Here the item will be saved.

Everything is still fine.

Now, in the last Review (post) action, I got the following code at the end:

 return RedirectToAction("Review", "Ad"); 

This will trigger the first Review action again, the problem is that it will provide the previous identifier? I thought RedirectToAction would not provide any parameters?

+4
source share
1 answer

I thought RedirectToAction would not provide any parameters?

Your understanding is wrong. Parameters present in modelstate are automatically forwarded if the destination URL contains a route parameter with the same name. In this case, you have an id parameter that is published (probably as part of the input field or part of the URL), and when you redirect back to the original index action, because your route definitions have an id token at the end of the RedirectToAction method will fill it.

As a workaround to avoid this behavior, you can explicitly indicate that the id parameter should not be sent when redirecting:

 return RedirectToAction("Index", "Ad", new { id = "" }); 
+4
source

All Articles