Reputation: 4531
This is how I want my routes to work:
http://example.com -> UpdatesController.Query()
http://example.com/myapp/1.0.0.0 -> UpdatesController.Fetch("myapp", "1.0.0.0")
http://example.com/myapp/1.0.0.1 -> UpdatesController.Fetch("myapp", "1.0.0.1")
http://example.com/other/2.0.0.0 -> UpdatesController.Fetch("other", "2.0.0.0")
My controller looks like this:
public class UpdatesController : Controller {
public ActionResult Query() { ... }
public ActionResult Fetch(string name, string version) { ... }
}
The route config that I've tried is this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Fetch",
url: "{name}/{version}",
defaults: new { controller = "Updates", action = "Fetch" },
constraints: new { name = @"^.+$", version = @"^\d+(?:\.\d+){1,3}$" }
);
routes.MapRoute(
name: "Query",
url: "",
defaults: new { controller = "Updates", action = "Query" }
);
But only the first example works. The others that should call the Fetch
action method all fail with 404.
What am I doing wrong?
(I've also tried it without the route constraints, but there is no difference)
Upvotes: 1
Views: 38
Reputation: 616
add following code to web.config, because your url contains dot value (1.0.0.0)
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
Another way
you can do it via Attribute routing.
to enable attribute routing, write below code in RouteConfig.cs
routes.MapMvcAttributeRoutes();
In controller your Action look like this
[Route("")]
public ActionResult Query()
{
return View();
}
[Route("{name}/{version}")]
public ActionResult Fetch(string name, string version)
{
return View();
}
Upvotes: 1