C # MVC 4 - Action gives Error 404 [closed]

0

I'm a beginner in C # MVC 4 and I set up a website for learning purposes.

On the contact page of the site there is a form that automatically sends emails. When I squeeze it locally straight from Visual Studio 2013 (IIS Express) it sends the emails quietly and loads the view of "success".

All right until I upload to the Locaweb server. Now it gives a 404 error:

  

Server Error in '/ user / site' Application.

     

The resource can not be found.

     

Description: HTTP 404. The resource you are looking for (or one of its   dependencies) could have been removed, had its name changed, or   temporarily unavailable. Please review the following URL and make   sure that it is spelled correctly.

     

Requested URL: / username / site / en / Contact / Sent /

     

Version Information: Microsoft .NET Framework Version: 4.0.30319;   ASP.NET Version: 4.0.30319.34280

The big problem is that the views are all there. I ran the internet and they told me that I could call the view with the direct path, but it also did not work: (

In this case I do not understand if what he is not finding is Action or View return View() .

Here is the action that is in my contact page controller:

[HttpPost]
        public ActionResult Sent(Formulario formulario)
        {
            if (formulario.Validar())
            {
                try
                {
                    var mensagem = formulario.Mensagem.Replace(Environment.NewLine, "<br/>");
                    var smtpUri = ConfigurationManager.AppSettings["smtpUri"];
                    var smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
                    var emailTo = ConfigurationManager.AppSettings["emailTo"];

                    var client = new SmtpClient(smtpUri, smtpPort)
                    {
                        EnableSsl = false,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential("meuEmail", "minhaSenha")
                    };
                    var mail = new MailMessage("meuEmail, emailTo);
                    mail.IsBodyHtml = true;
                    mail.Subject = String.Format("Contato via site - Página: {0}", formulario.Pagina);
                    mail.Body = mensagem;
                    client.Send(mail);

                    return View("~/Views/Contact/Sent.cshtml");
                }
                catch (Exception e)
                {
                    return View("~/Views/Contact/Error.cshtml");
                }
            }

            return Json(new {success = false});
        }

My RouteConfig.cs:

 public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("i18n", "{language}/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional, language = "pt-br" },
                new { language = "en|pt|es|pt-br|en-us" }
                );

            routes.MapRoute("Default", "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
        }
    }

In fact "/usuario/site" is because the site is in the air inside a subfolder because I do not want it to be in the air with errors yet. It works smoothly on every page, except when I try to email the contact page.

Does anyone have any idea what it can be ?! Thank you !!

    
asked by anonymous 14.12.2015 / 18:25

2 answers

1

From what I've seen, this view will be rendered only if the verb used is POST. try adding the [HttpGet] attribute and post it again to see if it works.

Another thing caught my attention in your url: / username / site / Contact / Sent /

Is this correct?

Generally we use something more simplified like " link "

In your case, I think it would be / Contact (Controller) and Action / Sent

    
14.12.2015 / 18:39
0

I solved my problem by adding a forward slash ("/") to the end of the call URL!

    
15.08.2017 / 23:14