您无法通过属性路由完成类似的事情。只能通过实现来进行高级路由匹配IRouteConstraint
或子类化RouteBase
.
在这种情况下,子类化更简单RouteBase
。这是一个例子:
public class EndsWithRoute : RouteBase
{
private readonly Regex urlPattern;
private readonly string controllerName;
private readonly string actionName;
private readonly string prefixName;
private readonly string parameterName;
public EndsWithRoute(string controllerName, string actionName, string prefixName, string parameterName)
{
if (string.IsNullOrWhiteSpace(controllerName))
throw new ArgumentException($"'{nameof(controllerName)}' is required.");
if (string.IsNullOrWhiteSpace(actionName))
throw new ArgumentException($"'{nameof(actionName)}' is required.");
if (string.IsNullOrWhiteSpace(prefixName))
throw new ArgumentException($"'{nameof(prefixName)}' is required.");
if (string.IsNullOrWhiteSpace(parameterName))
throw new ArgumentException($"'{nameof(parameterName)}' is required.");
this.controllerName = controllerName;
this.actionName = actionName;
this.prefixName = prefixName;
this.parameterName = parameterName;
this.urlPattern = new Regex($"{prefixName}/[^/]+/?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var path = httpContext.Request.Path;
// Check if the URL pattern matches
if (!urlPattern.IsMatch(path, 1))
return null;
// Get the value of the last segment
var param = path.Split('/').Last();
var routeData = new RouteData(this, new MvcRouteHandler());
//Invoke MVC controller/action
routeData.Values["controller"] = controllerName;
routeData.Values["action"] = actionName;
// Putting the myParam value into route values makes it
// available to the model binder and to action method parameters.
routeData.Values[parameterName] = param;
return routeData;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
object controllerObj;
object actionObj;
object parameterObj;
values.TryGetValue("controller", out controllerObj);
values.TryGetValue("action", out actionObj);
values.TryGetValue(parameterName, out parameterObj);
if (controllerName.Equals(controllerObj.ToString(), StringComparison.OrdinalIgnoreCase)
&& actionName.Equals(actionObj.ToString(), StringComparison.OrdinalIgnoreCase)
&& !string.IsNullOrEmpty(parameterObj.ToString()))
{
return new VirtualPathData(this, $"{prefixName}/{parameterObj.ToString()}".ToLowerInvariant());
}
return null;
}
}
Usage
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new EndsWithRoute(
controllerName: "Template",
actionName: "Index",
prefixName: "templates",
parameterName: "templateFilename"));
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
这将匹配这些 URL:
http://localhost/templates/t1
http://localhost/foo/bar/templates/t2
并将它们都发送到TemplateController.Index()
方法以最后一段为templateFilename
范围。
NOTE:出于 SEO 目的,将相同内容放在多个 URL 上通常不被认为是一个好的做法。如果您这样做,建议使用规范标签 https://support.google.com/webmasters/answer/139066?hl=en通知搜索引擎哪个 URL 是权威性 one.
请参阅此内容以在 ASP.NET Core 中完成相同的任务 https://stackoverflow.com/a/49014426.