2

I have a TextObject controller, which is meant to be accessed by "~/umt/text/{action}/{id?}", as defined in the controller using attribute routing, but the action link:

@Html.ActionLink("Index", "Index", "TextObject")

ignores Attribute Routing and uses the Conventional routing definitions, producing ~/TextObject/ instead of the desired ~/umt/text/

the TextObjectController:

[Authorize]
[RouteArea("umt")]
[RoutePrefix("text")]
[Route("{action=index}/{id?}")]
public class TextObjectController : Controller
{
   .....
    public async Task<ActionResult> Index()
    {
        return View(await db.TextObjects.ToListAsync());
    }
   .....
}

My route config:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //Enable Attribute Routing
        routes.MapMvcAttributeRoutes();

        AreaRegistration.RegisterAllAreas();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Is there any additional configuration required on the controller to make the action link work or does it not work with attribute routing?

I'd like to keep it simple, and it routes correctly going directly through the url, but the ActionLink helper seems to not like something about it.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
Ricardo
  • 23
  • 3

1 Answers1

0

I can't see that you specify your defauld area it RouteConfig so your action link should look like:

@Html.ActionLink("TextObject", "Index", "Index", new { area = "umt" }, null)
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • Thank you. Works fine. But, out of curiosity, is it possible to make the Attribute Routes the default? – Ricardo Nov 19 '15 at 06:35
  • @Ricardo yes it is, you should do it in `RegisterRoutes` check [this answer](http://stackoverflow.com/a/17688156/1849444). – teo van kot Nov 19 '15 at 06:36