HTTP 404.No found in [HTTPPOST] of the driver

1

Good morning

I'm starting on asp.net and I'm having a problem that I can not find the solution for, because I do not understand why it works exactly.

What happens is that when you send a form to the controller, the actionResult receives an object.

 [HttpPost]// con esta linea se presenta el problema.
    public ActionResult SinRedimensionar(TrabajaImagen imagen)
    {


        if (imagen.Fichero != null && imagen.Fichero.ContentLength > 0)
        {

            try
            {
                Image.FromStream(imagen.Fichero.InputStream);

                string nombreFichero = Path.GetFileNameWithoutExtension(imagen.Fichero.FileName) + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(imagen.Fichero.FileName);
                imagen.Fichero.SaveAs(Path.Combine(Server.MapPath("~/Imagenes"), nombreFichero));
                imagen.NombreFichero = nombreFichero;
            }
            catch 
            {
                ModelState.AddModelError(string.Empty, "La imagen seleccionada no es una imagen válida");
            }

        }
        else
        {
            ModelState.AddModelError(string.Empty, "No se ha seleccionado ninguna imagen");
        }

        return View(imagen);
    }

When I add the [HttpPost] I get the error of not found 404. but if I remove it, no. And I can not understand why this is happening, how could this be solved and how is the correct way to make it work?

It should be clear that from the view if I am sent information by the post method.

@using (Html.BeginForm("SinRedimensionar", "Fichero", null, FormMethod.Post, new { enctype = "multipart/form-data", role = "form" }))

{     

@Html.HiddenFor(m => m.NombreFichero)

<div class="col-md-12">
    <input id="btnSubmit" type="submit" value="Subir" />
</div>

@Html.ValidationSummary(false)

} I appreciate your help.

    
asked by usernovell 09.08.2016 в 17:14
source

1 answer

1

When you send the object to the controller in an action

public ActionResult SinRedimensionar(TrabajaImagen imagen);

The object is sent by POST, at least, in the examples that I do, if in the form I tell it to be sent by POST, it is not necessary to place the [HttpPost] in the controller since the sending of the form seeks the action by POST.

By sending the whole object you can work directly with the data entered in the form, without having to retrieve them by the request. If you could put the whole view or if you have defined another controller with the same for the GET maybe I could help you with the problem that comes up, I hope it serves you.

    
answered by 19.12.2016 в 17:34