C# MVC 4 - Action gives Error 404

Asked

Viewed 1,802 times

0

I am a beginner in C# MVC 4 and have set up a site for learning purposes.

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

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

Server Error in '/user/site' Application.

The Resource cannot 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 is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /usuario/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 reviewed the internet and I was told that I could call the view with the direct way, but it also did not work :(

In this case I did not understand if what he is not thinking is the Action or the View of return View().

Follow the action 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 the "/usuario/site" is because the site is on the air within a subfolder because I don’t want it to be on the air with errors yet. It is working smoothly on all pages, except when trying to send an email through the contact page.

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

  • You could put the routes you’re using here?

  • 1

    I updated the question with the route settings, Paulo. Thank you!

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

1 answer

1

From what I saw, this view will be rendered only if the verb used is the POST. try adding the [Httpget] attribute and publish again to see if it works.

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

This /en is correct?

Usually we use something more simplified as "http://www.site.com.br/NomeDoController/NomeDaAction"

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

  • Thanks for the reply Thiago! Actually I want to use POST even... And as locally it is working I see no reason to switch to GET just for that reason. Anyway, I updated the question with explanations about the URL, if you can check I will be very grateful! abs

  • Do the following, create a new Action in this same controller that returns a Content: public Actionresult Test() { Return Content("TEST");}

  • and try to access the url: www.seusite.com.br/usuario/site/en/Contact/Test

  • www.seusite.com.br/usuario/site/en/Contact/Teste or www.seusite.com.br/site/en/Contact/Teste or www.seusite.com.br/site/en/Contact/Teste one of these must return the content www.seusite.com.br/en/Contact/Teste

  • I was able to access via http://meusite.com.br/usuario/site/Contact/Teste/ I put a GET method also that only renders the view, and in the POST method I put a Redirecttoaction calling the GET method. Now the view is being shown, but the email is not sent :(

  • tried switching smtp? Try sendgrid.... should work

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.