Question

I have a put and post method in controller that when they work, they would normally redirect to the get method. But I noticed that using the redirect process in MVC is slower than just returning the call of the get method.

Sample from MS code:

return RedirectToRoute("someRoute", routeVarWithId);

What I found that takes less time:

return Get(Id);

Since my put, post, and get, all return IHttpActonResult, I don't see why I should use the redirect if the call is within my one controller, and the security rights are the same.

Am I missing anything obvious here?

Was it helpful?

Solution

But I noticed that using the redirect process in MVC is slower than just returning the call of the get method.

Of course it is; there are now two requests.

Am I missing anything obvious here?

Imagine I submit a form as a POST request to order a new computer from your website. Instead of returning a redirect to my order page, it just renders it out. Then my cat jumps on the keyboard and hits CTRL+R (refresh). What happens? My browser resubmits the last request, which was the POST. Now I've ordered two computers!

Instead, after successfully processing the POST request, you should return a redirect to the order page, which my browser will fetch with a GET. Now I can refresh to my heart's content and nothing bad will ever happen.

This also gives the user the ability to bookmark the page or email it to my wife. You can't email links that are POST requests.

For some light reading on the topic, refer to the HTTP/1.1 standard, specifically section 9.5 and following:

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

So when a POST creates a new resource, like an Order, it should return a 201 Created redirect to the URL where the new resource (i.e. the order) can be retrieved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top