3

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.

1 Answer 1

0

Area is not a route value that is stored in the RouteData.Values collection, it is instead found on the RouteData.DataTokens collection (as metadata). It is not correct to set a default for area in your RouteCollection because this only gets applied to RouteData.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 your MapRoute.

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "UnitGroups", action = "List", id = UrlParameter.Optional }
);
Sign up to request clarification or add additional context in comments.

1 Comment

I got around to trying this today, but its still the same. I still have Area= as a querystring.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.