Exemple #1
0
 /// <summary>
 /// Resolves and navigates to the given route request.
 /// </summary>
 /// <param name="request">The request.</param>
 public void ProcessRequest(NavigationRequest request)
 {
     if (request.Uri == null)
     {
         ExecuteRequestWithoutUri(request.RouteData);
     }
     else
     {
         ExecuteRequestWithUri(request.Uri, request.RouteData);
     }
 }
        /// <summary>
        /// Resolves and navigates to the first route that matches the route values for the given view model.
        /// </summary>
        /// <typeparam name="TViewModelOrController">The type of the view model.</typeparam>
        /// <param name="navigator">The navigator.</param>
        /// <param name="parameters">The parameters.</param>
        public static void Navigate <TViewModelOrController>(this INavigator navigator, object parameters)
        {
            var routeValues = new RouteValueDictionary(parameters);

            if (typeof(IController).IsAssignableFrom(typeof(TViewModelOrController)))
            {
                routeValues["controller"] = GetControllerNameFromType(typeof(TViewModelOrController));
            }
            else
            {
                routeValues["viewModel"] = typeof(TViewModelOrController).Name.Replace("ViewModel", "");
            }

            var request = new NavigationRequest(routeValues);

            navigator.ProcessRequest(request);
        }
        /// <summary>
        /// Navigates to the specified controller, following the action and parameters passed below. Note
        /// that the controller must be registered in the controller factory with the type name minus the
        /// "Controller" suffix - for example, a controller of type "PatientSearchController" should be
        /// registered with the name "PatientSearch" for this call to work.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="navigator">The navigator.</param>
        /// <param name="actionSelector">The action selector.</param>
        public static void Navigate <TController>(this INavigator navigator, Expression <Func <TController, ActionResult> > actionSelector)
            where TController : IController
        {
            var routeValues    = new RouteValueDictionary();
            var controller     = typeof(TController);
            var controllerName = GetControllerNameFromType(controller);

            routeValues.Add("controller", controllerName);

            var body = actionSelector.Body as MethodCallExpression;

            if (body == null)
            {
                throw new ImpossibleNavigationRequestException("The lambda expression used for navigation could not be parsed. The lambda should be a MethodCallExpression, for example: 'x => x.Search(text)'.");
            }

            var method     = body.Method;
            var actionName = method.Name;

            routeValues.Add("action", actionName);

            var parameters = method.GetParameters();
            var arguments  = body.Arguments;

            for (var i = 0; i < parameters.Length && i < arguments.Count; i++)
            {
                var parameter = parameters[i].Name;
                var argument  = arguments[i];
                var lambda    = Expression.Lambda <Func <TController, object> >(Expression.Convert(argument, typeof(object)), actionSelector.Parameters.ToList());
                var compiled  = lambda.Compile();
                var value     = compiled(default(TController));

                routeValues.Add(parameter, value);
            }

            var request = new NavigationRequest(routeValues);

            navigator.ProcessRequest(request);
        }