asp.net mvc - How to make optional parameter url in Mvc -
dont't vote negative if cant solve prob because know answer thats why iam here @ end.
controller
[route("{name}")] public actionresult details(int? id, string name) { if (id == null) { return new httpstatuscoderesult(httpstatuscode.badrequest); } menu menu = db.menus.find(id); if (menu == null) { return httpnotfound(); } return view(menu); }
views
@html.actionlink("details", "details", new { id = item.id, name = item.menuname })
route.config.cs
route.mapmvcattributeroutes();
output:
how output this
localhost:2345/blog instead of localhost:2345/details/id=1?name=blog
you can't because url in routeconfig has specific format: {controller}/{action}/{id}
to url want, may create public actionresult blog()
public class routeconfig { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "noscript", url : "noscript", defaults : new { controller = "home", action = "enablejavascript"} ); routes.maproute( name: "default", url: "{controller}/{action}/{id}",//the specific format defaults: new { controller = "home", action = "index", id = urlparameter.optional } ); } }
Comments
Post a Comment