简体   繁体   中英

ASP .Net custom route not working

ASP .Net custom route not working.

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //default route
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
        );

       //custom route
        routes.MapRoute(
         "Admin",
         "Admin/{addressID}",// controller name with parameter value only(exclude parameter name)
         new { controller = "Admin", action = "address" }
       new { addressID = @"\d+" }
     );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }


    public ActionResult address(int addressID = 0)
    {
     //code and redirection
    }

Here I want to hide everything from the url if possible...like i want to hide action name and parameter name and value if possible... Suggest me the possible way to do this

Like I want URL like this (on priority basis)
1.http: //localhost:abcd/Admin
or 2.http: //localhost:abcd/Admin/address
or 3.http: //localhost:abcd/Admin/1
or 4.http: //localhost:abcd/Admin/address/1

for quick reference.

  • the custom route should appear before the default.
  • try naming your custom rout as null. routes.MapRoute( null, // Route name...
  • check that your calling the correct action.
  • if youre dealing with actions that dont recieve a parameter upon initial load(example paging) makesure that your parameter is nullable address(int? addressID) and on your custom route it should be like this

//custom route
    routes.MapRoute(
     null, //<<--- set to null
     "Admin/{addressID}",// controller name with parameter value only(exclude arameter name)
     new { controller = "Admin", action = "address" }
   //new { addressID = @"\d+" } <<--- no need for this because based from your example " 2.http: //localhost:abcd/Admin/address" the parameter can be null.
 );

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM