0

I have several controllers for widgets named {WidgetName}WidgetController, e.g. SampleWidgetController. I need to create a route which catches all requests to such controllers and passes them to one common controller together with requested controller's name and action.

public class SampleWidgetController : Controller
{
    public ActionResult Content()
    {
        ...
    }
}

public class CommonController : Controller
{
    public ActionResult Content(string controllerName, string actionName)
    {
        // I want all requests to SampleWidget/Content to be passed here
        // With controllerName = "SampleWidget" and actionName = "Content"
    }
}

I can create a custom RouteConstraint to accept only those controllers that have 'Widget' suffix, but I have a problem with defining the route itself which will pass the requested controller's name and action to the common controller.

1
  • why do you want to do this..?? do you plz tell the purpose ..? Commented May 7, 2015 at 7:21

1 Answer 1

1

In your RouteConfig RegisterRoutes method add the following route before the default:

routes.MapRoute("Widgets", "{controllerName}Widget/{actionName}",
            new { controller = "Common", action="Content"});

This will cause incoming requests matching the format you've specified, e.g. [baseurl]/testWidget/testaction would hit your CommonController Content Action with a controllerName="test" and actionName="testaction"

If needed you can then append the "Widget" back onto the controllerName variable and pass it into your desired handler / do what you're trying to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.