Mike Cole
Mike Cole

Reputation: 14703

Determining the controller from a raw URL in ASP.NET MVC4

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?

Upvotes: 1

Views: 418

Answers (1)

Tommy
Tommy

Reputation: 39807

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.

Upvotes: 2

Related Questions