Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add namespace to custom route extension

Im using routes.add instead of routes.maproute (which has a namespace arg) because I extended the Route Class. I need to add namespace on the routes because one of my Areas has the same controller name within the site. My problem is I dont know where to put the namespace..


public class CultureRoute : Route
{
     public CultureRoute(string url, object defaults, object constraints, RouteValueDictionary dataTokens)
            : base(url, new RouteValueDictionary(constraints), dataTokens, new MvcRouteHandler())
        {
        }
}

Global.asax


routes.Add("Default", new CultureRoute(
    "{controller}/{action}/{id}",
    new {controller = "Home", action = "Index", id = UrlParameter.Optional}));

like image 616
h3n Avatar asked Nov 05 '10 05:11

h3n


People also ask

What is namespace routing?

In its simplest form, a Namespace Routing Language (NRL) schema consists of a mapping from namespace URIs to schema URIs. An NRL schema is written in XML. DSDL Part 4 (ISO/IEC 19757-4), NVDL is based on NRL.

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

What is custom route in MVC?

A custom route constraint can also be used with a Convention based routing. The new version MVC has an override version MapRoute method that accepts a constraint as a parameter. Using this method we can pass over a custom constraint.


1 Answers


var dataTokens = new RouteValueDictionary();
var ns = new string[] {"MyProject.Controllers"};
dataTokens["Namespaces"] = ns;

routes.Add("Default", new CultureRoute(
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    null /*constraints*/,
    dataTokens
));

like image 183
h3n Avatar answered Sep 23 '22 08:09

h3n