1

I am new to ASP.NET MVC RAZOR and I am trying to implement a File Upload to my page. I found many questions concerning this topic but I have an error and I can´t figure out why. This is my form in my view:

    @using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ 
    <input type="file" name="file" />
    <input type="submit" name="Submit" id="Submit" value="Upload" />   

}

And this is my Controller:

namespace Upload.Controllers
{
    public class UploadController : Controller
    {
        //
        // GET: /Upload/

        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);                    
                var path = Path.Combine("C:\\temp\\", fileName);
                file.SaveAs(path);                
            }
            return RedirectToAction("Index"); ;
        }
    }
}

When I run my page I get an error, which says: "Ressource not found: "/Upload". Where is my mistake? Sorry, I know I am a beginner in ASP.NET but I read many tutorials and just want this to work. Thanks a lot.

1

1 Answer 1

1

Your controller is named Upload, but your action also. You'll have to use /Upload/Upload/ as URL, or change the Upload actions to Index as the latter is the default action.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, I implemented the upload method in the home Controller and now it´s working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.