Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关于大量路线的建议_C#_.net_Asp.net Mvc 4_Asp.net Routing - Fatal编程技术网

C# 关于大量路线的建议

C# 关于大量路线的建议,c#,.net,asp.net-mvc-4,asp.net-routing,C#,.net,Asp.net Mvc 4,Asp.net Routing,在我们的RouteConfig中,为了将公司配置文件名称作为我们的一些根URL,我们为每个公司名称添加了一个路由,例如xyz.com/acme 虽然这看起来效果不错,但我希望能得到一些关于这方面的一般性建议,或者找到一个更好的解决方法。我特别关注业绩,尤其是随着我们的发展,我们可能有10000多家公司,因此有10000多条航线 代码如下所示: foreach (string companyName in companyNameList) { routes.MapRoute(

在我们的RouteConfig中,为了将公司配置文件名称作为我们的一些根URL,我们为每个公司名称添加了一个路由,例如xyz.com/acme

虽然这看起来效果不错,但我希望能得到一些关于这方面的一般性建议,或者找到一个更好的解决方法。我特别关注业绩,尤其是随着我们的发展,我们可能有10000多家公司,因此有10000多条航线

代码如下所示:

foreach (string companyName in companyNameList)
{
    routes.MapRoute(
        name: companyName,
        url: companyName,
        defaults: new { controller = "CompanyProfile", action = "Index" });
}
internal class CompanyConstraint: IRouteConstraint
{
    public CompanyConstaint(IList<string> companies)
    {
        this.companies = companies;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        object company;
        if (!values.TryGetValue("company", out company) || company == null)
        {
             return false;
        }

        return companies.Contains(company.ToString());
    }
}

如果您有任何建议,我们将不胜感激。

您能否没有以下形式的单一路线:

        routes.MapRoute(
            name: "Default",
            url: "{company}/{controller}/{action}/{id}",
            defaults: new { controller = "CompanyProfile", action = "Index", id = UrlParameter.Optional },
            constraints: new { company= new CompanyConstraint(companyNames) });
其中
CompanyConstraint
是一个
IRoutConstraint
,用于检查以确保公司名称有效?大概是这样的:

foreach (string companyName in companyNameList)
{
    routes.MapRoute(
        name: companyName,
        url: companyName,
        defaults: new { controller = "CompanyProfile", action = "Index" });
}
internal class CompanyConstraint: IRouteConstraint
{
    public CompanyConstaint(IList<string> companies)
    {
        this.companies = companies;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        object company;
        if (!values.TryGetValue("company", out company) || company == null)
        {
             return false;
        }

        return companies.Contains(company.ToString());
    }
}
内部类公司约束:IRouteConstraint
{
上市公司污染(IList公司)
{
这是指公司;
}
公共布尔匹配(HttpContextBase httpContext、路由路由、字符串参数名称、RouteValueDictionary值、RouteDirection RouteDirection)
{
目标公司;
如果(!values.TryGetValue(“公司”,out company)| | company==null)
{
返回false;
}
return companys.Contains(company.ToString());
}
}
干杯,
Dean

您不能有以下形式的单一路线:

        routes.MapRoute(
            name: "Default",
            url: "{company}/{controller}/{action}/{id}",
            defaults: new { controller = "CompanyProfile", action = "Index", id = UrlParameter.Optional },
            constraints: new { company= new CompanyConstraint(companyNames) });
其中
CompanyConstraint
是一个
IRoutConstraint
,用于检查以确保公司名称有效?大概是这样的:

foreach (string companyName in companyNameList)
{
    routes.MapRoute(
        name: companyName,
        url: companyName,
        defaults: new { controller = "CompanyProfile", action = "Index" });
}
internal class CompanyConstraint: IRouteConstraint
{
    public CompanyConstaint(IList<string> companies)
    {
        this.companies = companies;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        object company;
        if (!values.TryGetValue("company", out company) || company == null)
        {
             return false;
        }

        return companies.Contains(company.ToString());
    }
}
内部类公司约束:IRouteConstraint
{
上市公司污染(IList公司)
{
这是指公司;
}
公共布尔匹配(HttpContextBase httpContext、路由路由、字符串参数名称、RouteValueDictionary值、RouteDirection RouteDirection)
{
目标公司;
如果(!values.TryGetValue(“公司”,out company)| | company==null)
{
返回false;
}
return companys.Contains(company.ToString());
}
}
干杯,
Dean

最好确保
IList
是一个
HashSet
,以便
.Contains
尽可能快地运行!最好确保
IList
HashSet
,以便
.Contains
尽可能快地运行!