MVC routing issue to make two methods work

52 Views Asked by At

I have product controller and two methods edit and fileupload. what should I do in my route config to make these two work.

Product/6 (for editing productid 6)

Product/Fileupload (for uploading file).

my current route in routeconfig is as follows:

     routes.MapRoute(
        name: "editProducts",
        url: "Product/{id}",
        defaults: new { controller = "Product", action = "Edit", id=UrlParameter.Optional }
    );
3

There are 3 best solutions below

0
On

Add tahe following routes by same order before the default.

    routes.MapRoute(
        name: "fileupload",
        url: "{controller}/{action}",
        defaults: new { controller = "Product", action = "Fileupload"}
    );

    routes.MapRoute(
        name: "editProducts",
        url: "Product/{id}",
        defaults: new { controller = "Product", action = "Edit", id=UrlParameter.Optional }
    );
2
On

Try following:

routes.MapRoute(
        name: "editProducts",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Product", action = "Edit", id=UrlParameter.Optional }
    );
1
On

Add the following routes (in this order) before the default. I have assumed that you always need an ID to edit so the id parameter is not optional (but you could make it so) and I was not sure if you needed to pass a parameter to the FileUpload method

routes.MapRoute(
  name: "Upload",
  url: "Product/FileUpload/{id}",
  defaults: new { controller = "Product", action = "FileUpload", id = UrlParameter.Optional }
);

routes.MapRoute(
  name: "editProducts",
  url: "Product/{id}",
  defaults: new { controller = "Product", action = "Edit" }
);