1

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,

Community
  • 1
  • 1
Dimitri
  • 922
  • 2
  • 13
  • 34

1 Answers1

2

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" }

                );
        }
    }
}
Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61