如何自定义路由动态URL映射到ASP.NET MVC3一个名为组成控制器 [英] How to customize route to map url dynamically to a composed named controller in ASP.NET MVC3
问题描述
我需要映射这样的网址:
I need to map URLs like this:
/股票/风险 - > StockRiskController.Index()
/stock/risk -->StockRiskController.Index()
/股票/风险/ ATTR - > StockRiskController.Attr()
/stock/risk/attr -->StockRiskController.Attr()
/ srock /风险/图 - > StockRiskController.Chart()
/srock/risk/chart -->StockRiskController.Chart()
...
/债券/性能 - > BondPerformanceController.Index()
/bond/performance -->BondPerformanceController.Index()
/债券/性能/ ATTR - > BondPerformanceController.Attr()
/bond/performance/attr -->BondPerformanceController.Attr()
/债券/性能/图 - > BondPerformanceController.Chart()
...
/bond/performance/chart -->BondPerformanceController.Chart()
...
第一部分是动态的,但枚举的,第二部分只有两种选择(风险|性能)
The first part is dynamic but enumerable, the second part has only two options(risk|performance).
现在我知道只有两种方式:
For now I know only two ways:
- 订做的ControllerFactory(似乎overkilled或复杂的)
- 硬code所有的组合,因为它们是枚举(丑陋的)。
我可以使用 routes.MapRoute
来实现这一目标?或任何其他方便的方式?
Can I use routes.MapRoute
to achieve this? Or any other handy way?
推荐答案
有是基于 IRouteConstraint
一个很好的解决方案。首先,我们必须创建新的路由映射:
There is a nice solution based on IRouteConstraint
. First of all we have to create new route mapping:
routes.MapRoute(
name: "PrefixedMap",
url: "{prefix}/{body}/{action}/{id}",
defaults: new { prefix = string.Empty, body = string.Empty
, action = "Index", id = string.Empty },
constraints: new { lang = new MyRouteConstraint() }
);
下一步是建立我们的约束。之前,我将介绍一些方法如何检查相关如上所述 - 两个列表的可能值,但逻辑可以调整
Next step is to create our Constraint. Before I will introduce some way how to check relevance as mentioned above - two list with possible values, but logic could be adjusted
public class MyRouteConstraint : IRouteConstraint
{
public readonly IList<string> ControllerPrefixes = new List<string> { "stock", "bond" };
public readonly IList<string> ControllerBodies = new List<string> { "risk", "performance" };
...
而现在的比赛方式,因为我们需要将调整路由
And now the Match method, which will adjust the routing as we need
public bool Match(System.Web.HttpContextBase httpContext
, Route route, string parameterName, RouteValueDictionary values
, RouteDirection routeDirection)
{
// for now skip the Url generation
if (routeDirection.Equals(RouteDirection.UrlGeneration))
{
return false;
}
// try to find out our parameters
string prefix = values["prefix"].ToString();
string body = values["body"].ToString();
var arePartsKnown =
ControllerPrefixes.Contains(prefix, StringComparer.InvariantCultureIgnoreCase) &&
ControllerBodies.Contains(body, StringComparer.InvariantCultureIgnoreCase);
// not our case
if (!arePartsKnown)
{
return false;
}
// change controller value
values["controller"] = prefix + body;
values.Remove("prefix");
values.Remove("body");
return true;
}
您可以使用此方法的更多上场,但现在这个概念应该是清楚的。
You can play with this method more, but the concept should be clear now.
的注:我很喜欢你的方法。有时它只是更重要的是扩展/调整路由然后转到code和修复名。类似的解决方案是在这里工作:动态修改RouteValueDictionary 的
这篇关于如何自定义路由动态URL映射到ASP.NET MVC3一个名为组成控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!