I have the following route defined:
var route = new Route("{id}/{a}/{b}", new MvcRouteHandler());
route.Defaults = new RouteValueDictionary(new { controller = "Home", action = "Show" });
route.Defaults.Add("a", "");
route.Defaults.Add("b", "");
And the following controller code:
public ActionResult Show(int id)
{
RouteValueDictionary routeValues = new RouteValueDictionary();
routeValues["Controller"] = "Home";
routeValues["Action"] = "Show";
routeValues["id"] = 1;
var requestContext = new RequestContext(this.HttpContext, RouteData);
var rv = route.GetVirtualPath(requestContext, routeValues);
// when targetting .NET 4 rv is null, when its 3.5 it is "/1"
}
Why does this code return a route in .NET 3.5 and not in .NET 4.0?
Why are you mixing a
and b
with Controller
and Action
in your route? Since Controller
and Action
are required by the routing engine, I suggest you stick with them. The following example works:
var route = new Route("{Id}/{Controller}/{Action}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary
{
{ "Id", "" },
{ "Controller", "Home" },
{ "Action", "Show" },
}
};
ActionResult Show(int id)
{
RouteValueDictionary routeValues = new RouteValueDictionary();
routeValues["Controller"] = "Home";
routeValues["Action"] = "Show";
routeValues["Id"] = 1;
var requestContext = new RequestContext(this.HttpContext, RouteData);
var rv = route.GetVirtualPath(requestContext, routeValues);
// rv.VirtualPath == "1".
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With