2

I'm attempting to specify a route in MVC4 that ignores a controller name if presented with a specific action name. For example.

mysite.com/AnyControllerNameHere/SpecificAction - Would let me specify the controller and action to use while

mysite.com/AnyControllerNameHere/NotSpecificAction - Would take me the the AnyControllerNameHere Controller and NotSpecificAction method like the MVC default.

I've attempted to code something up but it doesn't seem to do what I want. Full route config is just the MVC defaults plus me attempting to do this so...

    public static void RegisterRoutes(RouteCollection routes)
    {
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

       routes.MapRoute(
            "SpecificAction",
            "{controller}/SpecificAction",
            new { controller = "Home", action = "SpecificAction" }
            );


        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
4
  • The order of the route declaration does matter. If your "SpecificAction" is before the default route declaration it should work. So please your full route config! Commented Apr 6, 2013 at 18:36
  • So is the only way to do this to put SpecificAction before the {controller}? I could get that to work but that's not what I want to do. Commented Apr 6, 2013 at 18:52
  • What is it that you're expecting it to do that's different to the default routing? You have told it explicitly to go to SpecificAction in the first route, so it'll do exactly what the default routing would do in the same scenario... are you sure it's not working exactly how you want it to? Commented Apr 6, 2013 at 19:13
  • With what I have if I go to mysite.com/home/specificaction I get to that action but when I go to mysite.com/someothercontrollername/specificaction I don't. Does that help explain what I'm hoping to do? I want to ignore the {controller} if the URL's Action is SpecificAction. Commented Apr 6, 2013 at 19:19

3 Answers 3

5

When you write this:

routes.MapRoute(
    "SpecificAction",
    "{controller}/SpecificAction",
    new { controller = "Home", action = "SpecificAction" });

you intend to override the controller. However, the third argument cannot be used to override parameters. It merely provides the defaults for any parameters that aren't already provided by the URL.

So what you need is a route template which doesn't set the controller parameter, so that the default takes effect:

routes.MapRoute(
    name: "SpecificAction",
    url: "{ignored}/SpecificAction",
    defaults: new { controller = "Home", action = "SpecificAction" });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that does exactly what I want and was easy to understand.
1

The easiest way to do this would be to use a Route constraint.

routes.MapRoute(
    "SpecificAction",
    "{anyController}/{specificAction}",
    new {controller="Home", action="SpecificAction"},
    new {specificAction = "SpecificAction" }
 );

The key here is that you don't using the default {controller} and {action} but rather something different (they could be anything), but use the route constraint to lock it to the SpecificAction action.

Wim's method works as well.

Comments

0

I don't believe that this can be easily achieved with MVC's built-in routing. You could consider writing a custom route handler or, alternatively, do something like the following to have the default route do the job for you:

public class BaseController : Controller
{
    public ActionResult SpecificAction()
    {
        return RedirectToAction("SpecificAction", "Home");
    }
}

public class SomeController : BaseController
{
    /* ... */
}

public class HomeController : BaseController
{
    public override ActionResult SpecificAction()
    {
        /* Do whatever */
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.