Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alistair Jenkins 92 posts 315 karma points
    Nov 24, 2017 @ 11:31
    Alistair Jenkins
    0

    Syntax error in controller?

    Hi,

    Based on a couple of guides I found I have created a model and controller in my app_code directory, but they crash my site. I've been looking at them for a while now, but I don't know enough to work out what's going wrong.

    Model: ContactFormViewModel.cs

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Web;
    
    namespace ClearviewForm.Models
    {
    public class ContactFormViewModel {
    
    
        public string Name { get; set; }
    
    
        public string Email { get; set; }
    
    
        public string Message { get; set; }
    }
    }
    

    Controller: ContactFormController.cs

    using ClearviewForm.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace ClearviewForm.Controllers
    {
    public class ContactFormController : SurfaceController
    {
    
        /// <summary>
        /// Renders the Contact Form
        /// @Html.Action("RenderContactForm","ContactFormSurface");
        /// </summary>
        /// <returns></returns>
        public ActionResult RenderContactForm()
        {
            //Return a partial view ContactForm.cshtml in /views/ContactForm/ContactForm.cshtml
            //With an empty/new ContactFormViewModel
            return PartialView("ContactForm", new ContactFormViewModel());
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult HandleContactForm(ContactFormViewModel model)
        {
            //Check if the data posted is valid (All required's & email set in email field)
            if (!ModelState.IsValid)
            {
                //Not valid - so lets return the user back to the view with the data they entered still prepopulated
                return CurrentUmbracoPage();
            }
    
            //Generate an email message object to send
            MailMessage email   = new MailMessage(model.Email, "[email protected]");
            email.Subject       = "Contact Form Request";
            email.Body          = model.Message;
    
            try
            {
                //Connect to SMTP credentials set in web.config
                SmtpClient smtp = new SmtpClient();
    
                //Try & send the email with the SMTP settings
                smtp.Send(email);
            }
            catch (Exception ex)
            {
                //Throw an exception if there is a problem sending the email
                throw ex;
            }
    
            //Update success flag (in a TempData key)
            TempData["IsSuccessful"] = true;
    
            //All done - lets redirect to the current page & show our thanks/success message
            return RedirectToCurrentUmbracoPage();
        }
      }
    }
    

    Any help gratefully appreciated,

    Cheers, Alistair

  • Matt Darby 28 posts 389 karma points c-trib
    Nov 24, 2017 @ 12:20
    Matt Darby
    0

    Hey Alistair,

    Is there any error in the logs?

    A guess - are you missing using System.Net.Mail in your controller?

  • Alistair Jenkins 92 posts 315 karma points
    Nov 24, 2017 @ 12:59
    Alistair Jenkins
    0

    Hi Matt,

    I added using System.Net.Mail; in to my controller and tried again, but still get the ysod.

    The error that appears at the bottom of the log file directly after doing that is as follows:

    _shutDownMessage=Change Notification for critical directories.
    App_Code dir change or directory rename
    HostingEnvironment initiated shutdown
    Change Notification for critical directories.
    App_Code dir change or directory rename
    Change Notification for critical directories.
    App_Code dir change or directory rename
    HostingEnvironment caused shutdown
    
    _shutDownStack=   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
     at System.Environment.get_StackTrace()
     at System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal()
     at System.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand()
     at System.Web.HttpRuntime.ShutdownAppDomain(String stackTrace)
     at System.Web.HttpRuntime.OnCriticalDirectoryChange(Object sender, FileChangeEvent e)
     at System.Web.FileChangesMonitor.OnCriticaldirChange(Object sender, FileChangeEvent e)
     at System.Web.DirectoryMonitor.FireNotifications()
     at System.Web.Util.WorkItem.CallCallbackWithAssert(WorkItemCallback callback)
     at System.Web.Util.WorkItem.OnQueueUserWorkItemCompletion(Object state)
     at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
     at System.Threading.ThreadPoolWorkQueue.Dispatch()
     at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies