c# - Problems with creating two routes which won't generate 404 error in ASP.NET MVC -
i'm trying build tutorial project routing. main objective build 2 routes won't generate 404 error in case. mean if path wrong want routing use /home/index path. have 2 following routes -
routes.maproute("default", "{controller}/{action}", new {controller = "home", action = "index"} ); routes.maproute("second", "{*catchall}", new {controller = "home", action = "index", id = urlparameter.optional} );
it works fine when use nonexistent path doesn't matches first route, -
but if does, have following -
or
i understand reason why happens. @ present moment, managed find 'some sort' of solution. adding following code web.config file -
<customerrors mode="on"> <error statuscode="404" redirect="~/home/index"/> </customerrors>
however, don't think best way solve problem. because, far can understand, catches errors , redirects correct path, without using routing. don't think need global handling.
so please give me hint or provide me solution problem. thank you.
well, didn't define "wrong" routing is, here definition:
- the controller or action not exist in project.
- if "id" passed, must exist on action method.
routes
i used constraints this. afaik, not possible use constraint on optional parameter. means in order make id
parameter optional, need 3 routes.
routes.maproute( name: "defaultwithid", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index" }, constraints: new { action = new actionexistsconstraint(), id = new parameterexistsconstraint() } ); routes.maproute( name: "default", url: "{controller}/{action}", defaults: new { controller = "home", action = "index" }, constraints: new { action = new actionexistsconstraint() } ); routes.maproute( name: "second", url: "{*catchall}", defaults: new { controller = "home", action = "index" } );
actionexistsconstraint
public class actionexistsconstraint : irouteconstraint { public bool match(httpcontextbase httpcontext, route route, string parametername, routevaluedictionary values, routedirection routedirection) { if (routedirection == routedirection.incomingrequest) { var action = values["action"] string; var controller = values["controller"] string; var thisassembly = this.gettype().assembly; type[] types = thisassembly.gettypes(); type type = types.where(t => t.name == (controller + "controller")).singleordefault(); // ensure action method exists return type != null && type.getmethod(action) != null; } return true; } }
parameterexistsconstraint
public class parameterexistsconstraint : irouteconstraint { public bool match(httpcontextbase httpcontext, route route, string parametername, routevaluedictionary values, routedirection routedirection) { if (routedirection == routedirection.incomingrequest) { var action = values["action"] string; var controller = values["controller"] string; var thisassembly = this.gettype().assembly; type[] types = thisassembly.gettypes(); type type = types.where(t => t.name == (controller + "controller")).singleordefault(); var method = type.getmethod(action); if (type != null && method != null) { // ensure parameter exists on action method var param = method.getparameters().where(p => p.name == parametername).firstordefault(); return param != null; } return false; } return true; } }
Comments
Post a Comment