Reputation: 15488
I want to map all calls that look like mydomain.com/ng* to my Ng Controller index. I'm not getting how to add that MapRoute. I've tried what is below but that give me an immediate error The route parameter name '' is invalid.
// ng*
routes.MapRoute("NgWildcard", "Ng{*}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
Upvotes: 0
Views: 3329
Reputation: 56869
From MSDN:
The URL pattern consists of segments that come after the application name in an HTTP request. For example, in the URL
http://www.contoso.com/products/show/beverages
, the pattern applies to products/show/beverages. A pattern with three segments, such as {controller}/{action}/{id}, matches the URLhttp://www.contoso.com/products/show/beverages
. Each segment is delimited by the / character. When a segment is enclosed in braces ( { and } ), the segment is referred to a URL parameter. ASP.NET routing retrieves the value from the request and assigns it to the URL parameter. In the previous example, the URL parameter action is assigned the value show. If the segment is not enclosed in braces, the value is treated as a literal value.
From MSDN:
You can define more than one placeholder between delimiters, but they must be separated by a constant value. For example, {language}-{country}/{action} is a valid route pattern. However, {language}{country}/{action} is not a valid pattern, because there is no constant or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the language placeholder from the value for the country placeholder.
In other words, "Ng{*}" is treated as a literal value in the URL, since you have neither enclosed the entire segment in braces nor you have defined 2 placeholders with a separator in between. In order to have it treated as a placeholder, you must put a valid separator character between the Ng
and the {*}
. As Daniel mentioned, you should also name the wildcard placeholder so you can reference it in your route.
routes.MapRoute("NgWildcard", "Ng-{*ngName}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
If you want your route to be exactly per your requirements, you will need to inherit RouteBase.
public class NgRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
var path = httpContext.Request.Path;
if (path.StartsWith("Ng"))
{
// Process your route
result.Values["controller"] = "Ng";
result.Values["action"] = "Index";
// Process additional URL segments and set route values accordingly
}
// Always return null in case of a non-match.
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData result = null;
var controller = Convert.ToString(values["controller"]);
var action = Convert.ToString(values["action"]);
if (controller == "Ng" && action == "Index")
{
// Return the virtual path to your URL (not sure how you will do that)
var virtualPath = "Ng"; // TODO: Finish path
return new VirtualPathData(this, virtualPath);
}
// Always return null in case of a non-match.
return result;
}
}
And as others have already stated, you are on your own as far as parsing other segments out of the URL when using a wildcard.
Upvotes: 2
Reputation: 190943
Try naming it
routes.MapRoute("NgWildcard", "Ng{*ngName}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
Upvotes: 0