0

I have defined Details method in Pro controller and i want to map this method to multiple types of url.

Please check below code, i have tried on another method with one parameter and it is working but in this case it's not working with multiple optional parameter.

[Route("Vehicles/Details/{no}/{aTab?}/{rs?}/{fp?}")]
[Route("Pro/Details/{no}/{aTab?}/{rs?}/{fp?}")]
[Authorization(SA.PMV, Rights.View)]
public ActionResult Details(string no, string aTab, string rs, bool fp = false)
{
    return View();
}

Error Showing in debugging:

A public action method 'Details' was not found on controller 'ProController'.

when I request the following URL:

http://www.example.com/Pro/Details?no=sT90Fjts0qI$&aTab=viewdetails&rs=viewdetails&fp=false
Nkosi
  • 235,767
  • 35
  • 427
  • 472

3 Answers3

0

The routes should be placed in order from lowest to highest parameters

public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                //enabling attribute routing
                routes.MapMvcAttributeRoutes();
    
                // 
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
    
                routes.MapRoute(
                    name: "Admin",
                    url: "{controller}/{action}/{did}/{docType}",
                    defaults: new { controller = "Home", action = "Index2", did = UrlParameter.Optional, docType = UrlParameter.Optional }
                );
    
                routes.MapRoute(
                    name: "User",
                    url: "{controller}/{action}/{uid}/{docId}/{typeId}",
                    defaults: new { controller = "Home", action = "Index3", uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
                );
    
            }
        }

Int? means that it may not require the data to be displayed

    public ActionResult Index(int? id)
            {
                ViewBag.Id = id;
                return View();
            }
    
    
            public ActionResult Index2(int? did, int? docType)
            {
                ViewBag.Id = did;
                ViewBag.doc = docType;
                return View();
            }
            public ActionResult Index3(int? uid, int? docId, int? typeId)
            {
                ViewBag.Id = uid;
                ViewBag.doc = docId;
                ViewBag.tipo = typeId;
                return View();
            }
Kuro Neko
  • 795
  • 12
  • 19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 20 '23 at 06:54
0

Mvc attribute routing issue with multiple parameters in Areas

this Works for me, in ASP.NET 4.8 MVC and API WEB

public class STDAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "STD";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "STD_default",
                "STD/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

            context.MapRoute(
                "STD_R2param",
                "STD/{controller}/{action}/{did}/{docType}",
                new { action = "Index", id = UrlParameter.Optional, docType = UrlParameter.Optional }
            );

        }
    }

this controller
    public class HomeController : Controller
    {
        // GET: PSTD/Home
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Index2(string did, int? docType)
        {
            ViewBag.Id = did;
            ViewBag.doc = docType;
            return View();
        }

    }
Kuro Neko
  • 795
  • 12
  • 19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 20 '23 at 06:53
-2

Best way to define route is into route.config as per below

routes.MapRoute("namesearch","Details/{no}/{aTab}/{rs}/{fp}",
            new { controller = "controllerName", action = " Details", 
                 no = UrlParameter.Optional, aTab = UrlParameter.Optional,
                 rs = UrlParameter.Optional ,fp = UrlParameter.Optional});
Mehul Bhalala
  • 780
  • 7
  • 16