I want to get the current route name in my html helper but I don't know how to make it.
I thinks the solution is in this post post but I don't know how to create the RouteCollectionExtions.
For the moment my code is:
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; public static class RouteCollectionExtensions { public static Route MapRouteWithName(this RouteCollection routes, string name, string url, object defaults, object constraints) { Route route = routes.MapRoute(name, url, defaults, constraints); route.DataTokens = new RouteValueDictionary(); route.DataTokens.Add("RouteName", name); return route; } } public class RouteConfig { public static void RegisterRoutes(RouteCollectionExtensions routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("favicon.ico"); // Default routes.MapRoute ("Default", "", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); outes.MapRouteWithName("HomeFr", "fr", new { controller = "Home", action = "Index" }); } }
Do you know why the compiler says:
'MyNamespace.RouteCollectionExtensions' does not contain a definition for 'MapRoute' and the best extension method overload 'System.Web.Mvc.RouteCollection.Extensions.MapRoute(System.web.Routing.RouteCollection, string, string, string[])' has some invalid arguments.
Thanks you for your help. Regards,
In order to use the link you suggested, this is how your RouteConfig should look like:
(I dropped the constraints, because I didn't see he's using it in the example)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
public static class RouteCollectionExtensions
{
public static Route MapRouteWithName(this RouteCollection routes, string name, string url, object defaults)
{
Route route = routes.MapRoute(name, url, defaults);
route.DataTokens = new RouteValueDictionary();
route.DataTokens.Add("RouteName", name);
return route;
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRouteWithName(
"myRouteName",
"{controller}/{action}/{username}",
new { controller = "Home", action = "List" }
);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.