基本上,正如标题所说,当在ASP.NET MVC2.0中使用区域时,我最终得到了以下“MapRoute”处理代码被复制为“RouteCollection”和AreaRegistrationContext“。两个对象都在我的控制之外(即在.NET MVC框架中)但似乎没有来自任何公共接口/基类。在这种情况下,有人可以推荐一种方法来删除代码重复吗?
public void Map(RouteCollection routes)
{
if (Details != null)
{
if (Namespaces != null)
routes.MapRoute(Name, Url, Details, Namespaces);
else
{
routes.MapRoute(Name, Url, Details);
}
}
else
{
if (Namespaces != null)
routes.MapRoute(Name, Url, Namespaces);
else
{
routes.MapRoute(Name, Url);
}
}
}
public void Map(AreaRegistrationContext context)
{
if (Details != null)
{
if (Namespaces != null)
context.MapRoute(Name, Url, Details, Namespaces);
else
{
context.MapRoute(Name, Url, Details);
}
}
else
{
if (Namespaces != null)
context.MapRoute(Name, Url, Namespaces);
else
{
context.MapRoute(Name, Url);
}
}
}
答案 0 :(得分:0)
不确定这是否有帮助,但我实际上采用了与路线注册不同的方式。我没有以这种方式使用区域,而是实际创建了一个接口IRouteRegistrar
,我可以为其构建订单注册商。我的示例特定于MEF,但没有理由不能对任何其他IoC / SL实现使用相同的机制:
/// <summary>
/// Defines the required contract for implementing a route registrar.
/// </summary>
public interface IRouteRegistrar
{
#region Methods
/// <summary>
/// Registers any required routes.
/// </summary>
/// <param name="route">The route collection to register routes with.</param>
void RegisterRoutes(RouteCollection route);
#endregion
}
通过将路由注册抽象到这个简单的界面中,我发现它更加模块化和可测试。
/// <summary>
/// Registers any required routes with the routing system.
/// </summary>
[ExportBootstrapperTask("RegisterRoutes")]
public class RegisterRoutesBootstrapperTask : IBootstrapperTask
{
#region Methods
/// <summary>
/// Runs the task.
/// </summary>
/// <param name="container"></param>
public void Run(CompositionContainer container)
{
Throw.IfArgumentNull(container, "container");
var registrars = container
.GetExports<IRouteRegistrar, IOrderedMetadata>()
.OrderBy(r => r.Metadata.Order)
.Select(r => r.Value);
var routes = RouteTable.Routes;
foreach (var registrar in registrars)
registrar.RegisterRoutes(routes);
}
#endregion
}