Imagine a page where the user can update, delete, edit or even vote the input. Since each action will be handled by a different ActionResult, the url of the page will have to change in case there are errors. My question is simple: Say your page is “http://localhost/Input/” and when you there are some errors, I want to redirect to “http://localhost/Input/Error” and display an error message. I don’t want to use sessions for this therefore please don’t show me this link. Basically, want I want to do this is something similar:
public ActionResult Create(FormCollection form) {
try {
// some code here
return RedirectToAction("Index");
}
catch () {
string errorMessage = "You have done something bad!";
// and I want to pass this string to my Error Action.
return RedirectToAction("Error");
}
}
The Solution I’ve Used
I know that I said I don’t want to use TempData, but apparantly that is the best option and I’ve used that for my issue. As for the answer I’ve chosen DigBySwift’s answer because that’s the most logical thing you can do if you don’t want to use Session for this type of operation.
As you say, if you don’t want to use sessions then TempData is not an option. Ideally, you should try not to pass the error message to the next page. Instead store the error message(s) in a resource file.
You could pass an id/enum to you next page and then retrieve the error based on the passed parameter:
Edit: An alternative (since a db write-read would be expensive considering) would be to write the message to a cookie and then retrieve it after the redirect, and then delete the cookie. This is not great and I personally would not use this. But if you need to customise the message, it is an option.