Redirect breaks Function

Topics: Troubleshooting
Sep 21, 2011 at 7:46 AM
Edited Sep 21, 2011 at 7:58 AM

Hi there,

i'm working on/with the CyberStride.Contact module. First of all, this is the function i talk about:


        public ActionResult Create(int id, string returnUrl)
            var model = new ContactPartRecord();
            var form = Services.ContentManager.Get(id);
            if (InvalidForm(form))
                _notifier.Error(T("Form submission was not from a valid source"));
                return this.RedirectLocal(returnUrl, "~/");

            var contactForm = form.Parts.FirstOrDefault(p => p is ContactFormPart).As<ContactFormPart>();

            var req1 = Request["Name"];
            var req2 = Request["Email"];           

            if (TryUpdateModel(model, new[] { "Name", "Company", "Address", "Zipcode", "City", "Phone", "Email", "Message" }))
                if (req1 != "" && req1 != "Bitte ausf�llen..." && req2 != "" && req2 != "Bitte ausf�llen...")
                    var contact = _contactService.MakeContact(model);
                    Dictionary<string, string> messageProperties = mapProperties(contact);

                    _messenger.Send(contactForm.ContentItem.Record, MessageTypes.ContactRequest, "email", messageProperties);


//* my redirect here *//
 } else { populateTempDataWithViewModelValues(model); } } return this.RedirectLocal(returnUrl, "~/"); }



i didn't change too much, i've put in two vars ( req1 and 2 ) for my musthave fields, as validation doesn't really seem to work. So if i put in a "Response.Redirect("~/myurl")" in the ifclause that makes the contact, i get redirected if every input is all right, but it doesnt make a contact anymore. If i take it out the contact works, but ofc the redirect doesnt. I just cant figure out why this redirect breaks this code, since i placed it after the actual "makecontact" ...

Any ideas ?


EDIT: On a sidenote, in which Zone does the notifier send his Notice ? as they never seem to appear on my side, and the only Zone i didn't change is Content.


Sep 21, 2011 at 11:11 AM

Ok i figured the notify part out:

            <div id="main" role="main">
                @if (Model.Messages != null)
                    <div id="messages" class="zone">

adding this within layout content gives me the required zone.

This also removes the redirect problem, however i'd still like to know why the code stops working if there's a redirect in it.

Sep 21, 2011 at 5:31 PM

Probably because the notification is shorter-lived than that, or it may be a similar problem to redirecting after seetting a session variable. An ordinary redirect terminates the thread, which might cause things like cookies to never get set. There are ways to mitigate that, such as overloads that don't terminate the thread.