c# - 在 MVC 3 中实现 Controller 创建策略的最佳方式

标签 c# .net asp.net-mvc-3

我有一个 Controller 工厂,它根据路由值以不同的方式构建 Controller 。它们是:

  1. 如果路由值与“模块”路由匹配,它会使用路由值来计算合格的类型名称来确定正确的类型。也就是说,如果用户键入 [http://localhost:8080/siijyp/modules/Personas/Naturales/Documento/Display?id=1], Controller 工厂将构建并返回一个 OIMSIIJYP.Web。 *Personas.Naturales.Documento* Controller
  2. 如果路由值与“模块”路由匹配并且它还包含“脚本”值,则 Controller 工厂在运行时使用 Mono.CSharp 构建 Controller
  3. 如果路由值与模块不匹配,它会像 DefaultControllerFactory 那样构建 Controller 。

很明显所有这些 if 语句都是错误的,我也知道有一个 IControllerActivator 类型用于 DefaultControllerFactory 到创建给定 Controller 类型的 Controller 。但我不知道哪种是在 MVC 3 中实现这些 Controller 创建策略的最佳方式。

有什么想法吗?

代码:

public IController CreateController(RequestContext requestContext, 
    string controllerName)
{
    #region Argument checking
    if (requestContext == null)
{
        throw new ArgumentNullException("requestContext");
    }
    if (string.IsNullOrEmpty(controllerName))
    {
        throw new ArgumentException(
            "ControllerName cannot be null nor empty",     
            "controllerName");
    }
    #endregion
    Type controllerType = null;

    var routeData = requestContext.RouteData;
    if (IsRequestingAModule(routeData))
    {
        var module = routeData.Values["module"];
        var submodule = routeData.Values["submodule"];

        var controllerQualifiedName = string.Format(
            "OIMSIIJYP.Web.{0}.{1}.{2}Controller", module, 
            submodule, controllerName);
        try
        {
            controllerType = Type.GetType(controllerQualifiedName, true, 
                true);
            if (IsRequestingAScriptingBehavior(routeData))
            {
                return GetControllerRuntimeUsingMono(routeData, 
                    controllerName, controllerType);
            }
            else
            {
                return _container.Resolve(controllerType) as IController;
            }
        }
        catch (TypeLoadException)
        {
            throw new HttpException(404, 
                string.Format(CultureInfo.CurrentCulture, 
                "Controller not fount", new object[] {
                    requestContext.HttpContext.Request.Path
                }));
        }
    }
    else
    {
        var controllerTypes = 
            from type in Assembly.GetExecutingAssembly().GetTypes()
            where StringComparer.CurrentCultureIgnoreCase.Compare(
                type.Name, controllerName + "Controller") == 0
            select type;

        switch (controllerTypes.Count())
        {
            case 0:
                throw new HttpException(404, 
                    string.Format(CultureInfo.CurrentCulture, 
                    "Controller not found", new object[] {
                        requestContext.HttpContext.Request.Path
                    }));
            case 1:
                controllerType = controllerTypes.First();
                break;
            default:
                throw UnityControllerFactory.
                CreateAmbiguousControllerException(routeData.Route, 
                    controllerName, controllerTypes.ToList());
        }
    }

    return _container.Resolve(controllerType) as IController;
}

最佳答案

我认为它还有另一种解决方法,您可以使用Area。假设我有两个相同名称的 Controller ,但存储在两个不同的地方,其中一个在区域内,另一个是正常的,例如

admin/Controller/CommonController.cs //here admin is an area name
Controller/CommonController.cs

现在我有两个 Action 都试图渲染两个不同的 View 。

@Html.Action("SystemInfo", "Common", new { area = "Admin" }) // here Admin is the area name
@Html.Action("HeaderLinks", "Common")

可以使用路由获取相同的设施。

关于c# - 在 MVC 3 中实现 Controller 创建策略的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7869500/

相关文章:

asp.net-mvc - 使用 (Html.ValidationMessage) 对 mvc 中的下拉列表进行简单验证

asp.net - MiniProfiler.Stop() 上的 MVC Mini Profiler 异常

wpf - MVVM 和 MVC 之间共享模型?

c# - 应该使用 base 关键字吗?

c# - 具有多个类类型的单个组合 C# 列表

c# - 构建前和构建后事件参数

C# LINQ 问题,为什么这里需要 new?

c# - 如何在 WPF 中使按钮的透明部分可单击?

c# - 记录器提供程序 : How can I implement a custom formatter

C# { } 运算符