Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should i route POST and GET in Mvc 5

Tags:

I just started with routing in my web application and while i have read many tutorials on what routing is and how to write a RouteConfig.cs or use Attribute routing inside my controllers, very few tutorials really show how to handle a whole application.

Like most Asp.NET Mvc applications, when you scaffold your controller you get your create, edit, remove methods and you always have one to GET and one to POST .

But how do you handle the routing on these methods?

Say these are my Edit methods, what do i put on the post method for routing? And do i need to "fix" anything else for that routing to work when i click the "save" button?

   // GET: VehicleModels/Edit/5
    [Route("Aanbod/Edit/{id:int}")]
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        VehicleModels vehicleModels = db.VehicleModels
            .Include(v => v.Options)
            .Include(v => v.Fotos)
            .SingleOrDefault(v => v.Id == id);

        if (vehicleModels == null)
        {
            return HttpNotFound();
        }
        return View(vehicleModels);
    }

    // POST: VehicleModels/Edit/5        
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(int? id, IEnumerable<HttpPostedFileBase> uploads)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var VehicleToUpdate = db.VehicleModels.Find(id);
        if (TryUpdateModel(VehicleToUpdate))

        // my code here

        return View(VehicleToUpdate);

    }
like image 244
Vahx Avatar asked May 16 '16 13:05

Vahx


People also ask

What is routing in MVC 5 with example?

Routing is a pattern matching system. Routing maps an incoming request (from the browser) to particular resources (controller & action method). This means routing provides the functionality to define a URL pattern that will handle the request. That is how the application matches a URI to an action.

Which option is correct for attribute based routing in MVC 5?

Enabling attribute based routing With MVC 5 and attribute based routing, you gain finer control of your routes at both the controller and action level. Enabling attribute routing in your project is simple, just add a call to routes. MapMvcAttributeRoutes(); in your RegisterRoutes function.

How is the routing carried out in MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.

How can use route in MVC controller?

Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder. The following figure illustrates how to configure a route in the RouteConfig class .


1 Answers

If you're using attribute routing, the GET and POST actions will both have the same route attribute. The only thing that might be different is if you name your routes, in which case, the name should only go on the GET version. You can't apply the same name to two different routes, even if they're effectively the same route.

like image 91
Chris Pratt Avatar answered Nov 15 '22 03:11

Chris Pratt