Get Route Name in ASP.NET MVC Controller
In this article
To get the current route name in an ASP.NET MVC controller, access RouteData.Values["routeName"] or use the RouteData.Route property. The route name is the string you assigned when registering the route in RouteConfig.cs. This guide shows you exactly how to retrieve it, with code examples and common pitfalls.
What You'll Need
- An ASP.NET MVC project (any version, .NET Framework or .NET Core/5+).
- Basic familiarity with controllers and actions.
- Your routes defined in
RouteConfig.cs(orProgram.csfor .NET Core).
Step-by-Step
-
Understand how route names are defined.
In MVC, a route name is an optional string you provide when registering a route. For example, inRouteConfig.cs(classic MVC):routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );Here, the route name is
"Default". In .NET Core, you might useapp.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"). -
Access the route name in a controller action.
Inside any controller method, use theControllerContext.RouteDataproperty. The route name is stored inRouteData.Values["routeName"](note: the key is lowercaserouteName). For example:public ActionResult Index() { string routeName = RouteData.Values["routeName"] as string; ViewBag.RouteName = routeName ?? "No route name"; return View(); }If the route was registered without a name, this will be
null. -
Alternative: use
RouteData.Route.
If you need more details,RouteData.Routegives you theRouteobject. You can cast it toRouteand read itsNameproperty:var route = RouteData.Route as Route; string routeName = route?.Name;This works in both classic MVC and .NET Core, though the namespace differs (
System.Web.RoutingvsMicrosoft.AspNetCore.Routing). -
Handle cases where the route name is missing.
Not all requests come from a named route. For example, if you use attribute routing ([Route]), the route name is oftennullunless you explicitly setName = "..."in the attribute. Always check fornullbefore using the value. -
Use the route name in views or filters (optional).
You can also access it in a Razor view viaViewContext.RouteData.Values["routeName"], or in an action filter usingfilterContext.RouteData. The same logic applies.
Common Mistakes
- Using the wrong key case. The key is
"routeName"(lowercase 'r' and 'n'), not"RouteName". Using the wrong case returnsnull. - Assuming every request has a named route. If the route is defined without a name (e.g.,
routes.MapRoute("", ...)), the value will benull. Always handle that. - Confusing route name with controller/action names. The route name is the identifier you gave the route, not the controller or action.
- Forgetting that attribute routes don't have names by default. If you use
[Route]attributes, you must explicitly setNameto get a value.
Tips
- If you need the route name frequently, create a helper extension method:
public static string GetRouteName(this Controller controller) => controller.RouteData.Values["routeName"] as string; - In .NET Core, you can also use
HttpContext.GetRouteData()?.Values["routeName"]outside a controller, e.g., in middleware. - When debugging, set a breakpoint and inspect
RouteData.Valuesto see all available keys—this helps confirm the exact key name in your version. - Remember that route names are case-insensitive for matching, but the dictionary key is case-sensitive.
Troubleshooting
If the route name is always null:
- Check your route registration: did you actually provide a name? Look at your
MapRouteorMapControllerRoutecall. - If you're using attribute routing, add
Name = "myRoute"to the[Route]attribute. - Verify that the request is actually hitting the route you think it is. Use a debugger or temporarily output
Request.Urland the matched route template.
If you get a compile error about RouteData:
Make sure you have the correct using statements. In classic MVC, you need using System.Web.Routing; (though it's often implicit). In .NET Core, the namespace is Microsoft.AspNetCore.Routing.
If the route name is empty string instead of null:
Some MVC versions may store an empty string when no name is provided. Normalize with string.IsNullOrEmpty() checks.
Frequently Asked Questions
Can I get the route name in a view?
Yes, use ViewContext.RouteData.Values["routeName"] in Razor. It works the same way as in a controller.
Does the route name work with attribute routing?
Only if you explicitly set Name = "..." in the [Route] attribute. Otherwise, it's null.
Is the route name the same as the URL?
No. The route name is an internal identifier; the URL is generated from the route pattern. You can have multiple routes with the same URL pattern but different names.
Can I use the route name to generate URLs?
Yes, you can use Url.RouteUrl("routeName", new { ... }) to generate a URL based on a named route. This is a common use case.
What if I have two routes with the same name?
That's invalid—route names must be unique within an application. If you try to register duplicates, you'll get an exception at startup.
Does the route name appear in the URL?
No, the route name is not part of the URL. It's only used internally for identification and URL generation.