c# - After changing path using RouteConfig old path is not working -
i working on c# asp.net mvc project entity framework.
i trying change url path using routeconfig.cs
first code looked this
public class routeconfig { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "account", action = "login", id = urlparameter.optional } ); } } i wanted chenge url localhost/customer/index localhost/index
so added line of code , worked
routes.maproute( name: "customer", url: "customer", defaults: new { controller = "customer", action = "index"} ); next tried changing localhost/customer/details/2 localhost/customername/2.
// customername varies each customer so added this
routes.maproute( name: "customerdetail", url: "customer/{name}/{id}", defaults: new { controller = "customer", action = "details", id = urlparameter.optional, name = urlparameter.optional } ); now customer details page loads correctly.
but loading index page using path localhost/customer/index causes error, while localhost/customer causes no error. why happening?
edit
this how routeconfig looks like
public class routeconfig { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "customer", url: "customer", defaults: new { controller = "customer", action = "index"} ); routes.maproute( name: "customerdetail", url: "customer/{name}/{id}", defaults: new { controller = "customer", action = "details", id = urlparameter.optional, name = urlparameter.optional } ); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "account", action = "login", id = urlparameter.optional } ); } }
finally found answer.
as @stephenmuecke commented
localhost/customer/indexmatchescustomerdetailroute (and passes"index"id parameter)
so rechecked code , found id field in localhost/customer/details/2 not optional , accidently defined optional, id = urlparameter.optional.
routes.maproute( name: "customerdetail", url: "customer/{name}/{id}", defaults: new { controller = "customer", action = "details", name = urlparameter.optional } ); full routconfig code:
public class routeconfig { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "customer", url: "customer", defaults: new { controller = "customer", action = "index"} ); routes.maproute( name: "customerdetail", url: "customer/{name}/{id}", defaults: new { controller = "customer", action = "details",name = urlparameter.optional } ); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "account", action = "login", id = urlparameter.optional } ); } }
Comments
Post a Comment