0

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

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.