Asp.net mvc ASP.NET核心MVC中的虚线路由?

Asp.net mvc ASP.NET核心MVC中的虚线路由?,asp.net-mvc,asp.net-core,Asp.net Mvc,Asp.net Core,在Core之前,我在MVC中使用了如下内容: public class HyphenatedRouteHandler : MvcRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { requestContext.RouteData.Values["controller"] = requestCon

在Core之前,我在MVC中使用了如下内容:

 public class HyphenatedRouteHandler : MvcRouteHandler
    {
        protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
            requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
            return base.GetHttpHandler(requestContext);
        }
    }
如何在ASP.Net核心中在URL中使用破折号。。。喜欢并将其转换为我的友好url

我不想使用属性路由


谢谢

在ConfigureServices方法中的启动中添加此约定:

options.Conventions.Add(new DashedRoutingConvention());
UseMvc中的路由将不起作用。ASP.Net本身不会考虑它们。我在GitHub上创建了一个问题。。。但我不知道会怎样。现在,您可以使用方法上的属性指定路由。约定将重用/复制原始路由,并以
{controller}/{action}
格式更新/添加新的虚线路径

public class DashedRoutingConvention : IControllerModelConvention
{
    public void Apply(ControllerModel controller)
    {
        string parent = this.Convert(controller.ControllerName);

        foreach (ActionModel action in controller.Actions)
        {
            string child = this.Convert(action.ActionName);

            string template = $"{parent}/{child}";

            if (this.Lookup(action.Selectors, template) == true)
                continue;

            List<SelectorModel> selectors = action.Selectors.Where(item => item.AttributeRouteModel?.Template == null).ToList();
            if (selectors.Count > 0)
            {
                foreach (SelectorModel existing in selectors)
                {
                    if (existing.AttributeRouteModel == null)
                        existing.AttributeRouteModel = new AttributeRouteModel();

                    existing.AttributeRouteModel.Template = template;
                }
            }
            else
            {
                selectors = action.Selectors.Where(item => item.AttributeRouteModel?.Template != null).ToList();

                foreach (SelectorModel existing in selectors)
                {
                    SelectorModel selector = new SelectorModel(existing);

                    selector.AttributeRouteModel.Template = template;

                    if (action.Selectors.Any(item => this.Compare(item, selector)) == false)
                        action.Selectors.Add(selector);
                }
            }
        }
    }

    private string Convert(string token)
    {
        if (token == null)
            throw new ArgumentNullException(nameof(token));

        if (token == string.Empty)
            throw new ArgumentException("Failed to convert empty token.");

        return Regex.Replace(token, "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])", "-$1", RegexOptions.Compiled).Trim().ToLower();
    }

    private bool Lookup(IEnumerable<SelectorModel> selectors, string template)
    {
        foreach (SelectorModel selector in selectors)
        {
            string current = selector.AttributeRouteModel?.Template;

            if (string.Compare(current, template, StringComparison.OrdinalIgnoreCase) == 0)
                return true;
        }

        return false;
    }
    private bool Compare(SelectorModel existing, SelectorModel adding)
    {
        if (existing.AttributeRouteModel == null && adding.AttributeRouteModel != null)
            return false;

        if (existing.AttributeRouteModel != null && adding.AttributeRouteModel == null)
            return false;

        if (existing.AttributeRouteModel != null && adding.AttributeRouteModel != null)
        {
            if (existing.AttributeRouteModel.Template != adding.AttributeRouteModel.Template)
                return false;

            if (existing.AttributeRouteModel.Order != adding.AttributeRouteModel.Order)
                return false;
        }

        if (existing.ActionConstraints == null && adding.ActionConstraints != null)
            return false;

        if (existing.ActionConstraints != null && adding.ActionConstraints == null)
            return false;

        if (existing.ActionConstraints != null && adding.ActionConstraints != null)
        {
            if (existing.ActionConstraints.Count != adding.ActionConstraints.Count)
                return false;
        }

        return true;
    }
}
公共类DashedRoutingConvention:IControllerModelConvention
{
公共无效应用(控制器模型控制器)
{
字符串parent=this.Convert(controller.ControllerName);
foreach(controller.Actions中的ActionModel操作)
{
string child=this.Convert(action.ActionName);
字符串模板=$“{parent}/{child}”;
if(this.Lookup(action.Selectors,template)==true)
继续;
列表选择器=action.selectors.Where(item=>item.AttributeRouteModel?.Template==null.ToList();
如果(selectors.Count>0)
{
foreach(选择器中存在选择器模型)
{
if(existing.AttributeRouteModel==null)
existing.AttributeRouteModel=新的AttributeRouteModel();
existing.AttributeRouteModel.Template=模板;
}
}
其他的
{
selectors=action.selectors.Where(item=>item.AttributeRouteModel?.Template!=null.ToList();
foreach(选择器中存在选择器模型)
{
SelectorModel selector=新SelectorModel(现有);
selector.AttributeRouteModel.Template=模板;
if(action.Selectors.Any(item=>this.Compare(item,selector))==false)
action.selector.Add(选择器);
}
}
}
}
私有字符串转换(字符串标记)
{
if(标记==null)
抛出新ArgumentNullException(nameof(token));
if(标记==string.Empty)
抛出新ArgumentException(“转换空令牌失败”);
返回Regex.Replace(标记“(?)?