Asp.net mvc 忽略ASP.NET MVC核心路由中的第一段
我正在寻找能够匹配以下路线的路线定义:Asp.net mvc 忽略ASP.NET MVC核心路由中的第一段,asp.net-mvc,routes,.net-core,url-routing,asp.net-mvc-routing,Asp.net Mvc,Routes,.net Core,Url Routing,Asp.net Mvc Routing,我正在寻找能够匹配以下路线的路线定义: /segment/xxxx/def /segment/../xxxx/def /segment/that/can/span/xxxx/def 并且能够使用param`def运行动作xxxx 但这种路线是不允许的: [Route("/{*segment}/xxx/{myparam}")] 如何做到这一点?您可以使用自定义的IRouter与正则表达式相结合来进行类似这样的高级URL匹配 public class EndsWithRoute : IRout
/segment/xxxx/def
/segment/../xxxx/def
/segment/that/can/span/xxxx/def
xxxx
但这种路线是不允许的:
[Route("/{*segment}/xxx/{myparam}")]
如何做到这一点?您可以使用自定义的
IRouter
与正则表达式相结合来进行类似这样的高级URL匹配
public class EndsWithRoute : IRouter
{
private readonly Regex urlPattern;
private readonly string controllerName;
private readonly string actionName;
private readonly string parameterName;
private readonly IRouter handler;
public EndsWithRoute(string controllerName, string actionName, string parameterName, IRouter handler)
{
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(parameterName))
throw new ArgumentException($"'{nameof(parameterName)}' is required.");
this.controllerName = controllerName;
this.actionName = actionName;
this.parameterName = parameterName;
this.handler = handler ??
throw new ArgumentNullException(nameof(handler));
this.urlPattern = new Regex($"{actionName}/[^/]+/?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
var controller = context.Values.GetValueOrDefault("controller") as string;
var action = context.Values.GetValueOrDefault("action") as string;
var param = context.Values.GetValueOrDefault(parameterName) as string;
if (controller == controllerName && action == actionName && !string.IsNullOrEmpty(param))
{
return new VirtualPathData(this, $"{actionName}/{param}".ToLowerInvariant());
}
return null;
}
public async Task RouteAsync(RouteContext context)
{
var path = context.HttpContext.Request.Path.ToString();
// Check if the URL pattern matches
if (!urlPattern.IsMatch(path, 1))
return;
// Get the value of the last segment
var param = path.Split('/').Last();
//Invoke MVC controller/action
var routeData = context.RouteData;
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;
await handler.RouteAsync(context);
}
}
用法
此路由被参数化,以允许您传入与所调用的操作方法相对应的控制器、操作和参数名称
public class HomeController : Controller
{
public IActionResult About(string myParam)
{
ViewData["Message"] = "Your application description page.";
return View();
}
}
要使其与任何操作方法名称匹配,并能够使用该操作方法名称再次构建URL,还需要做更多的工作。但此路由允许您通过多次注册来添加其他操作名称
注意:出于搜索引擎优化的目的,通常认为在多个URL上放置相同的内容不是一个好的做法。如果您这样做了,建议使用通知通知搜索引擎哪个URL是权威URL
.catch all占位符不能位于路线模板末尾以外的任何位置。参考参考ok,但如何做到这一点?Nkosi刚才说你不能这样做没有其他选项?使用IRouter自定义路由?url重写?正则表达式?我不知道
public class HomeController : Controller
{
public IActionResult About(string myParam)
{
ViewData["Message"] = "Your application description page.";
return View();
}
}