My ASP.NET MVC project route seems to be ignoring the default when composing URLs For example, Url.Action ("index", "home")
return / home / index /
, while returning it only to /
There is no problem with url (which I have seen in all my other MVC websites) using , eg. By going to http: // myserver /
, the default controller and verb get correctly.
How can I correct the behavior? (Note how I have done this behavior get , but no one is suffering from this opposite problem.)
This project is not fantastic, no custom routing handler is. , And routing configuration is quite simple: "public-static zero register routs" {routes.IgnoreRoute ("{resource} .axd / { * PathInfo} "); Routes.IgnoreRoute ("LoginHandler"); ("Default", url: "{controller} / {action} / {* id}", default: new {controller = "home", action = "index", id = urlParameter.optional}). ; }
( *
is there because some actions use arbitrary strings in the form of identifiers which can include embedded slashes, and without asterisk, the path The URL does not match for this.)
The problem is actually in small asterisks UrlParameter.Optional
does not match against default, and there are completely full paths in the URL.
If you want to keep the default behavior, while holding the arbitrary parameter - using all the parameter syntax, you need to use the catch-all parameters in a secondary route :
public static zero registrations {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}"); Routes.IgnoreRoute ("LoginHandler"); Routes.MapRoute (name: "default", url: "{controller} / {action} / {id}", default: new {controller = "home", action = "index", id = urlparameter.option}}; Routes (.: "CatchAll", url: "{controller} / {action} / {* id}", default: new {controller = "home", action = "index", id = UrlParameter.Optional}). ; }
With this definition, simple parameters are not controlled using the first parameter (as the URL is the generation, which is not being specified as the correct path to use ), While the complex parameter with slash deterioration is back on the other path, and it is handled correctly.
(Note that this may only be a bug in the MVC implementation: Catch-For all parameters, MVC ParsedRoute.IsParameterRequired
returns null < / Code> as the default parameter value, which is tested with the default of root from the UrlParameter.Optional
supplied, and it is found individually.Therefore, if you change the route code Route definition changed to use default instead of id = UrlParameter.Optional
instead of id = null
. It also seems to work, but it seems to me that unwanted side effects can occur. I am also not sure whether this behavior has been deliberately done, or just a bug.)
< / Div>