예제 #1
0
        private static void AutoRegisterRoutes(
            IMvcApplication application,
            IServerRoutingTable serverRoutingTable,
            Mvc.Framework.DependencyConrainer.IServiceProvider serviceProvider)
        {
            var controllers = application.GetType().Assembly.GetTypes()
                              .Where(type => type.IsClass && !type.IsAbstract &&
                                     typeof(Controller).IsAssignableFrom(type));

            foreach (var controllerType in controllers)
            {
                var actions = controllerType
                              .GetMethods(BindingFlags.DeclaredOnly
                                          | BindingFlags.Public
                                          | BindingFlags.Instance)
                              .Where(x => !x.IsSpecialName && x.DeclaringType == controllerType)
                              .Where(x => x.GetCustomAttributes().All(a => a.GetType() != typeof(NonActionAttribute)));

                foreach (var action in actions)
                {
                    var path      = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{action.Name}";
                    var attribute = action.GetCustomAttributes().Where(
                        x => x.GetType().IsSubclassOf(typeof(BaseHttpAttribute))).LastOrDefault() as BaseHttpAttribute;
                    var httpMethod = HttpRequestMethod.Get;
                    if (attribute != null)
                    {
                        httpMethod = attribute.Method;
                    }

                    if (attribute?.Url != null)
                    {
                        path = attribute.Url;
                    }

                    if (attribute?.ActionName != null)
                    {
                        path = $"/{controllerType.Name.Replace("Controller", string.Empty)}/{attribute.ActionName}";
                    }

                    serverRoutingTable.Add(httpMethod, path,
                                           (request) => ProcessRequest(serviceProvider, controllerType, action, request));

                    Console.WriteLine(httpMethod + " " + path);
                }
            }
        }
예제 #2
0
        private static IHttpResponse ProcessRequest(
            Mvc.Framework.DependencyConrainer.IServiceProvider serviceProvider,
            System.Type controllerType,
            MethodInfo action,
            IHttpRequest request)
        {
            var controllerInstance = serviceProvider.CreateInstance(controllerType) as Controller;

            controllerState.SetState(controllerInstance);

            controllerInstance.Request = request;

            // Security Authorization - TODO: Refactor this
            var controllerPrincipal = controllerInstance.User;

            if (action.GetCustomAttributes()
                .LastOrDefault(a => a.GetType() == typeof(AuthorizeAttribute))
                is AuthorizeAttribute authorizeAttribute &&
                !authorizeAttribute.IsInAuthority(controllerPrincipal))
            {
                // TODO: Redirect to configured URL
                return(new HttpResponse(HttpResponseStatusCode.Forbidden));
            }

            var parameters      = action.GetParameters();
            var parameterValues = new List <object>();

            foreach (var parameter in parameters)
            {
                ISet <string> httpDataValue = TryGetHttpParameter(request, parameter.Name);

                if (parameter.ParameterType
                    .GetInterfaces()
                    .Any(i => i.IsGenericType &&
                         i.GetGenericTypeDefinition() == typeof(IEnumerable <>) &&
                         parameter.ParameterType != typeof(string)))
                {
                    var collection = httpDataValue.Select(x => System.Convert.ChangeType(
                                                              x, parameter.ParameterType.GenericTypeArguments.First()));

                    parameterValues.Add(collection);
                    continue;
                }

                try
                {
                    string httpStringValue = httpDataValue.FirstOrDefault();

                    var sss = parameter.ParameterType;

                    var parameterValue = System.Convert
                                         .ChangeType(httpStringValue, parameter.ParameterType);
                    parameterValues.Add(parameterValue);
                }
                catch
                {
                    var parameterValue = System.Activator.CreateInstance(parameter.ParameterType);

                    var properties = parameter.ParameterType.GetProperties();

                    foreach (var property in properties)
                    {
                        ISet <string> propertyHttpDataValue = TryGetHttpParameter(request, property.Name);

                        var firstValue = propertyHttpDataValue.FirstOrDefault();

                        var propertyValue = System.Convert.ChangeType(firstValue, property.PropertyType);

                        property.SetMethod.Invoke(parameterValue, new object[] { propertyValue });
                    }

                    if (request.RequestMethod == HttpRequestMethod.Post)
                    {
                        controllerState.Reset();

                        controllerInstance.ModelState = ValidateObject(parameterValue);

                        controllerState.Initialize(controllerInstance);
                    }

                    parameterValues.Add(parameterValue);
                }
            }

            var response = action.Invoke(controllerInstance, parameterValues.ToArray()) as ActionResult;

            return(response);
        }