1

Given the following URL: http://www.domain.com/Client

is it possible to access the Route Data in a controller to determine which Controller/Action that is bound to?

8
  • You can but it completely depends on where you want to access that data from..controller, authorize attribute, global.asax file...? Commented Apr 15, 2013 at 19:03
  • In a controller - question updated. Commented Apr 15, 2013 at 19:07
  • Ok, so I have got to ask...if you are in a controller...wouldn't you already know what controller you are in? Just trying to understand the logic you are trying to achieve. Commented Apr 15, 2013 at 19:08
  • No, because it's a partial action for my navigation. Commented Apr 15, 2013 at 19:09
  • 1
    I found the above that speaks to what you are trying to do. You will need to replace the fully qualified URL that you are trying to trace back to just a relative URL though Commented Apr 15, 2013 at 20:54

1 Answer 1

2

It should be pretty simple to determine the controller from the RouteData dictionary, passing the key that you are looking for.

namespace UI.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var controllerName = RouteData.Values["controller"];
            //controllerName == "Home" at this point
            var actionName = RouteData.Values["action"];
            //actionName == "Index" at this point         
            return View("Index");
        }

    }
}

EDIT

I have found some information regarding how to do this here: but, you will need to change your absolute URLs back to relative URLs before you can run them through the solution provided.

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

1 Comment

This shows the route for the current request. I need to lookup a URL.

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.