0

Let's say I have following simple controller:

public class DataController: Controller
{     
    [HttpGet]
    public ActionResult Index()
    {
        // some code
    }
}

Now I'd like Index action to be allways called if there is a GET request to DataContoller. I other words to ignore action name and any other parameters. For example all of following calls should be handled by Index action:

How can I achieve this?

2 Answers 2

3

You should update your RouteConfig like so:

public class RouteConfig
{
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute(
                name: "RouteOverwrite",
                url: "data/{*catchall}",
                defaults: new { controller = "Data", action = "Index" }
            );
        }
}

Make sure you use this in Application_Start:

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

// register route config
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

What's would be the difference using {catchall} instead of {*catchall} in this case?
@MaximeMatter I honestly can't recall.. I believe without the asterisk ASP.NET will try to parse it in as a parameter or something wonky like that. I do know that if you reference a named placeholder (i.e. {someVal}), and then have a route method with someVal as a parameter, it will parse the {someVal} part of the route out and fill in the parameter with that.
0

you can do this by using routetable. check out system.web.routing in asp.net mvc.

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.