RedirectToCurrentUmbracoPage() results in Connection error
Hi,
The surface controller for a form I have created:
using ContactForm.Models;
using System.Net.Mail;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
namespace ContactForm.Controllers
{
public class ContactFormController : SurfaceController
{
[HttpPost]
public ActionResult SendMail(ContactFormViewModel model)
{
if (!ModelState.IsValid)
return CurrentUmbracoPage();
if (ModelState.IsValid)
return SendMessage(model);
return Redirect();
}
private ActionResult SendMessage(ContactFormViewModel model)
{
string str = $"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd \"><html xmlns=\"http://www.w3.org/1999/xhtml \"><head></head><body><p>Name: {model.Name}</p> <p>Email: {model.Email}</p> <p>Message: {model.Message}</p></body></html>";
string sub = $"{model.Subject}";
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
msg.Subject = sub;
msg.Body = str;
msg.Priority = MailPriority.Normal;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Send(msg);
TempData.Add("Success", value: "Your message was successfully sent. We will get back to you as soon as possible");
return Redirect();
}
private ActionResult Redirect()
{
return RedirectToCurrentUmbracoPage();
}
public ActionResult ContactForm() { return PartialView(""); }
}
}
results in an
'Umbraco cannot start. A connection string is configured but Umbraco cannot connect to the database."
error. If I change RedirectToCurrentUmbracoPage() to just CurrentUmbracoPage(), there is no error, but of course the text entered into the form remains, which is what I am trying to avoid. Googled this for a bit but can't seem to find anyone having the same issue. Does anyone have any idea what the problem might be?
Still having this problem. My surface controller has changed a bit now due to implementing reCaptcha on the form, but I still have the same issue. Returning RedirectToCurrentUmbracoForm( ) at the end of SendMessage( ) results in an Umbraco cannot start error, while returning CurrentUmbracoPage( ) does not, but leaves the contents of the fields intact, which I want to avoid. Does anyone have any idea why this might be happening? Thanks, Alistair
using ContactFormView.Models;
using System.Net.Mail;
using System.Web.Mvc;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace ContactFormView.Controllers
{
public class ContactFormSurfaceController : SurfaceController
{
[HttpPost]
public ActionResult SendMail(ContactFormViewModel model)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
else
{
string EncodedResponse = Request.Form["g-Recaptcha-Response"];
bool IsCaptchaValid = (ReCaptchaClass.Validate(EncodedResponse) == "true" ? true : false);
if (!IsCaptchaValid)
{
if (EncodedResponse == "")
{
ModelState.AddModelError("reCAPTCHA", "Please complete the reCAPTCHA.");
return CurrentUmbracoPage();
}
else
{
ModelState.AddModelError("reCAPTCHA", "The reCAPTCHA is incorrect");
return CurrentUmbracoPage();
}
}
else
{
return SendMessage(model);
}
}
}
public class ReCaptchaClass
{
public static string Validate(string EncodedResponse)
{
var client = new System.Net.WebClient();
string PrivateKey = "6LfLjxcUAAAAAD3uofXIL0Evj8G3LgG5T04oz3RD";
var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse));
var captchaResponse = JsonConvert.DeserializeObject<ReCaptchaClass>(GoogleReply);
return captchaResponse.Success;
}
[JsonProperty("success")]
public string Success
{
get { return m_Success; }
set { m_Success = value; }
}
private string m_Success;
[JsonProperty("error-codes")]
public List<string> ErrorCodes
{
get { return m_ErrorCodes; }
set { m_ErrorCodes = value; }
}
private List<string> m_ErrorCodes;
}
private ActionResult SendMessage(ContactFormViewModel model)
{
string str = $"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd \"><html xmlns=\"http://www.w3.org/1999/xhtml \"><head></head><body><p>Name: {model.Name}</p> <p>Email: {model.Email}</p> <p>Message: {model.Message}</p></body></html>";
string sub = $"{model.Subject}";
string emailto = CurrentPage.GetPropertyValue<string>("emailAddressTo");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add(emailto);
msg.Subject = sub;
msg.Body = str;
msg.Priority = MailPriority.Normal;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Send(msg);
TempData.Add("Success", value: "Your message was successfully sent.");
TempData.Add("GetBack", value: "We will get back to you as soon as possible.");
return RedirectToCurrentUmbracoPage();
}
public ActionResult ContactForm() => PartialView("/Views/ContactForm/ContactForm.cshtml");
}
}
The site works fine without the controller being fired. The controller works fine if I use 'return CurrentUmbracoPage();'. The issue only occurs when I use 'return RedirectToCurrentUmbracoPage();' The solution may lie elsewhere I suppose but I don't know enough to know where and why and googling this hasn't made things any clearer.
RedirectToCurrentUmbracoPage() results in Connection error
Hi,
The surface controller for a form I have created:
results in an
error. If I change RedirectToCurrentUmbracoPage() to just CurrentUmbracoPage(), there is no error, but of course the text entered into the form remains, which is what I am trying to avoid. Googled this for a bit but can't seem to find anyone having the same issue. Does anyone have any idea what the problem might be?
All assistance gratefully received,
Cheers, Alistair
Hi.
Still having this problem. My surface controller has changed a bit now due to implementing reCaptcha on the form, but I still have the same issue. Returning RedirectToCurrentUmbracoForm( ) at the end of SendMessage( ) results in an Umbraco cannot start error, while returning CurrentUmbracoPage( ) does not, but leaves the contents of the fields intact, which I want to avoid. Does anyone have any idea why this might be happening? Thanks, Alistair
Is there really no-one who has any idea about this?
This is my connection string btw. Works ok the rest of the time.
Does the site work without this controller being fired?
Are you sure the connection issue is caused by the controller?
HI Mark,
The site works fine without the controller being fired. The controller works fine if I use 'return CurrentUmbracoPage();'. The issue only occurs when I use 'return RedirectToCurrentUmbracoPage();' The solution may lie elsewhere I suppose but I don't know enough to know where and why and googling this hasn't made things any clearer.
is working on a reply...