RouteData is a property of the base Controller class, so RouteData can be accessed in any controller. RouteData contains route information of a current request. You can get the controller, action or parameter information using RouteData as shown below.
Example: RouteData in MVC
public class StudentController : Controller
{
public ActionResult Index(int? id, string name, int? standardId)
{
var controller = RouteData.Values["controller"];
var action = RouteData.Values["action"];
id = (int)RouteData.Values["id"];
name = (string)RouteData.Values["name"];
standrdId = (int)RouteData.Values["standardId"];
var area = RouteData.DataTokens["areaname"];
return View();
}
}
Please note that you need to cast into appropriate data type or use implicitly typed variable- var.
Related Articles
- View in ASP.NET MVC
- How to bind a model to a partial view in ASP.NET MVC?
- How to define a custom action selector in ASP.NET MVC?
- How to display an error message using ValidationSummary in ASP.NET MVC?
- How to enable client side validation in ASP.NET MVC?
- How to enable bundling and minification in ASP.NET MVC?
- How to pre-compile razor view in ASP.NET MVC?
- How to set image path in StyleBundle?
- Difference between Html.RenderBody() and Html.RenderSection() in ASP.NET MVC
- Difference between Html.Partial() and Html.RenderPartial() in ASP.NET MVC
- How to use web.config customErrors in ASP.NET MVC?
- How to display a custom error page with error code using httpErrors in ASP.NET MVC?
- How to create a custom filter in ASP.NET MVC?
- Build e-commerce application on .NET Framework
- Redirect non-www to www domain in ASP.NET
- Redirect from HTTP to HTTPS in ASP.NET