How to use multiple action verb in mvc4

Total Post:52

Points:366

Mvc4 
C#
 1384  View(s)
Ratings:
Rate this:

How to use multiple action verb in mvc I don’t want to make get action method , I want to use get and post action verb in same action method and how we can know which action verb is called at current time

This is my sample code

Please anyone help me,

      public ActionResult image()
        {
            return View();
        }
       [HttpPost]
        public ActionResult Image(HttpPostedFileBase file)
        {
            if (file != null)
                try
                {
                    // extract only the fielname
                    var fileName = Path.GetFileName(file.FileName);
                    // store the file inside ~/Images folder
                    var path = Path.Combine(Server.MapPath("~/Images/file"), fileName);
                    file.SaveAs(path);                  
                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }
            return View();
         
        }

 

Thank you.

 

  1. Re: How to use multiple action verb in mvc4

    Hi Manish you can also apply multiple http verbs using AcceptVerbs attribute. GetAndPostAction method supports both, GET and POST ActionVerbs in the following example:


      [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
           
            public ActionResult Image(HttpPostedFileBase file)
            {
                if (file != null)
                    try
                    {
                        // extract only the fielname
                        var fileName = Path.GetFileName(file.FileName);
                        // store the file inside ~/Images folder
                        var path = Path.Combine(Server.MapPath("~/Images/file"), fileName);
                        file.SaveAs(path);                  
                        ViewBag.Message = "File uploaded successfully";
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = "ERROR:" + ex.Message.ToString();
                    }
                else
                {
                    ViewBag.Message = "You have not specified a file.";
                }
                return View();
             
            }

      Modified On Apr-11-2018 10:29:27 PM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.