0

I have MVC structure for my website. I try to keep my Controllers' name as clean and coherent as possible. But I wonder at how POST vs. GET controller should be named. For instance, assume I have a controller responsible for showing parts of the Admin Panel that deal with editing/creating images:

class AdminImageController extends Controller
{

}

While I have another controller that is responsible to post data from this section of the Admin Panel. Such as posting new images, posting edited images, anything that deal with POST type of request of this section:

class AdminImageController extends Controller
{

}

They both have the same name and thus would error and exit the script. What I want to do is how should I name them, to keep them consistent, coherent and clean, is adding the request verb useful? or is it common?

Such as this:

AdminGetImageController()

And for POST:

AdminPostImageController()

I don't want to have a discussion here, I just need to know what is standard in the industry (since it is out of experience), as naming conventions, if not properly served, make problems in advance.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105

1 Answers1

2

Your controllers will typically handle various requests, regardless if they're using GET or POST. Your controller is named properly, however your action method approach is a bit off.

Here's an example in C#:

public class AdminImageController : Controller
{
    [HttpGet] // only accepts GET requests
    public ActionResult Image()
    {
        // do stuff...

        return View();
    }

    [HttpPost] // only accepts POST requests
    public ActionResult Image(HttpPostedFileBase data)
    {
        // do stuff...

        // redirect back to the GET action
        return RedirectToAction("Image");
    }    
}

The [HttpGet] and [HttpPost] attributes are built into MVC and can be applied to individual actions or an entire controller.

keeehlan
  • 7,874
  • 16
  • 56
  • 104