I've tried adding the Route attribute to our controller through various methods.
[Route("Trials/{trialId:int}/Components/{action}")]
public partial class ComponentsController : Controller
{
public virtual ActionResult List(int trialId)
{
return View();
}
}
Or
[RoutePrefix("Trials/{trialId:int}/Components")]
[Route("{action=List}")]
public partial class ComponentsController : Controller
{
public virtual ActionResult List(int trialId)
{
return View();
}
}
are just a few examples.
The links generated going to this controller/action look like this:
http://localhost:50077/Trials/3/Components?Area=
I'm looking to remove the query string parameter. No matter how I place the route configuration with attributes, it never seems to work.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//routes.MapRoute(
// name: "TrialComponents",
// url: "Trials/{trialId}/Components/{action}/{id}",
// defaults: new {controller = "Components", action = "List", area = "", id = UrlParameter.Optional},
// constraints: new { trialId = "\\d+"}
//);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "UnitGroups", action = "List", area = "", id = UrlParameter.Optional }
);
}
}
The commented out route works and does not apply a querystring to the url.
Can anyone explain why the route method is adding the Area querystring and how I can fix it? I'm stumped.
Area is not a route value that is stored in the
RouteData.Values
collection, it is instead found on theRouteData.DataTokens
collection (as metadata). It is not correct to set a default forarea
in yourRouteCollection
because this only gets applied toRouteData.Values
of the request.In short, to remove the
Area
parameter from your generated URLs, you need to remove it as a default value in yourMapRoute
.