如何使用 ASP.NET MVC 制作各种通配符路线
How To Make a Wild Card Route of Sorts with ASP.NET MVC
我想将所有看起来像 mydomain.com/ng* 的调用映射到我的 Ng 控制器索引。我不知道如何添加该 MapRoute。我已经尝试了下面的内容,但立即出现错误 The route parameter name '' is invalid.
// ng*
routes.MapRoute("NgWildcard", "Ng{*}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
尝试命名它
routes.MapRoute("NgWildcard", "Ng{*ngName}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
来自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 URL http://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.
来自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.
换句话说,"Ng{*}" 在 URL 中被视为文字值,因为您既没有将整个段括在大括号中,也没有定义 2 个占位符,中间有分隔符。为了将其视为占位符,您必须在 Ng
和 {*}
之间放置一个有效的分隔符。正如丹尼尔提到的,您还应该命名通配符占位符,以便您可以在您的路线中引用它。
routes.MapRoute("NgWildcard", "Ng-{*ngName}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
如果你想让你的路线完全符合你的要求,你需要继承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;
}
}
正如其他人已经指出的那样,在使用通配符时,您只能靠自己来解析 URL 中的其他段。
我想将所有看起来像 mydomain.com/ng* 的调用映射到我的 Ng 控制器索引。我不知道如何添加该 MapRoute。我已经尝试了下面的内容,但立即出现错误 The route parameter name '' is invalid.
// ng*
routes.MapRoute("NgWildcard", "Ng{*}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
尝试命名它
routes.MapRoute("NgWildcard", "Ng{*ngName}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
来自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.
来自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.
换句话说,"Ng{*}" 在 URL 中被视为文字值,因为您既没有将整个段括在大括号中,也没有定义 2 个占位符,中间有分隔符。为了将其视为占位符,您必须在 Ng
和 {*}
之间放置一个有效的分隔符。正如丹尼尔提到的,您还应该命名通配符占位符,以便您可以在您的路线中引用它。
routes.MapRoute("NgWildcard", "Ng-{*ngName}",
new
{
/* Your default route */
controller = "Ng",
action = "Index"
});
如果你想让你的路线完全符合你的要求,你需要继承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;
}
}
正如其他人已经指出的那样,在使用通配符时,您只能靠自己来解析 URL 中的其他段。