This is a stripped-down example of the problem that I encountered today with ASP.NET MVC URL routing.
Quite simply, I need a route to call, regardless of whether a parameter was provided at the end.
This route works fine, matching both / apple / and / apple / test /
routes.MapRoute (
"Working Route",
"apple / {parameter}",
new {
controller = "Apple",
action = "Action",
parameter = UrlParameter.Optional
},
new {parameter = @ "([a-z0-9 \ .-] +)"}
);
However, this second route will only match / banana / test / and the like. When I try to run / banana / , the router just goes right above it and returns a 404 catch-all error.
routes.MapRoute (
Non-Working Route,
"banana / {parameter}",
new {
controller = "Banana",
action = "Action",
parameter = UrlParameter.Optional
},
new {parameter = @ "([a-z0-9] +)"}
);
The only difference is checking the Regex parameter, but since this is a pretty simple Regex match, they should work fine for a url like / banana / , but the second route just doesn't work to recognize it.
, Regex №2, №1, '.' '-', , -, .
EDIT:
, . .
public class AppleController : Controller
{
public ActionResult Action(string parameter)
{
if (parameter == null)
{
parameter = "No parameter specified.";
}
ViewData["parameter"] = parameter;
return View();
}
}
public class BananaController : Controller
{
public ActionResult Action(string parameter)
{
if (parameter == null)
{
parameter = "No parameter specified.";
}
ViewData["parameter"] = parameter;
return View();
}
}
, , /apple/ "No parameter defined.", /banana/ 404.
</" > ..
= URLParameter.Optional :
№ 1 , №2 .
= "" :
№1, №2 , URL.
= "" :
- .NET.
.