private Collection<ViewLocationMatch> ComputeViewMatches(ViewLocationContext context, IEnumerable<ViewLocation> locations) { var matches = new Collection<ViewLocationMatch>(); foreach (var location in locations) { if (!MatchsVerbMapping(context, location) && !MatchesActionName(context, location)) { continue; } var match = new ViewLocationMatch { Location = location }; var actionDescriptor = context.ActionDescriptor as ApiActionDescriptor; // Try to use ApiActionDescriptor as the Filters could have enriched/modified the original at this point. Type returnType = actionDescriptor != null ? actionDescriptor.ReturnType : context.ReturnType; if (location.IsCollection) { var innerType = QueryTypeHelper.GetQueryableInterfaceInnerTypeOrNull(returnType); if (innerType != null) { returnType = innerType; match.Incrememt(); } } var parameters = actionDescriptor.GetParameters(); if (parameters != null) { foreach (var parameter in location.ActionParameters) { if (parameters.Any(p => p.ParameterName.Equals(parameter.Key))) { match.Incrememt(); } } } if (returnType.Equals(location.Type)) { match.Incrememt(); } matches.Add(match); } return matches; }
public ViewResult SelectView(ViewLocationContext context) { var action = context.ActionDescriptor.ActionName; var controller = context.ActionDescriptor.ControllerDescriptor.ControllerName; var matches = ComputeViewMatches(context, this.Locations); ViewLocation location = SelectBestLocation(matches); if (location == null) { throw new InvalidOperationException( string.Format("Could not find view location for action '{0}' on controller '{1}'. Make sure you have views for the specified action.", context.ActionDescriptor.ActionName, controller)); } return new ViewResult { ViewName = location.ViewName }; }
/// <summary> /// Locates a view based on the provided information. /// </summary> /// <param name="viewName">The name of the view to locate.</param> /// <param name="model">The model that will be used with the view.</param> /// <param name="viewLocationContext">A <see cref="ViewLocationContext"/> instance, containing information about the context for which the view is being located.</param> /// <returns>A <see cref="ViewLocationResult"/> instance if the view could be found, otherwise <see langword="null"/>.</returns> public ViewLocationResult GetViewLocation(string viewName, dynamic model, ViewLocationContext viewLocationContext) { var fullName = string.Concat(viewName, ".sshtml"); var stream = GetBodyStream(fullName); return GetViewLocationResult(fullName, stream); }
/// <summary> /// Renders a view to a response object, bypassing content negotiation. /// </summary> /// <param name="context">Current Nancy context</param> /// <param name="viewName">View name</param> /// <param name="model">Model object (or null)</param> /// <returns>Response object containing the rendered view (if found)</returns> public Response RenderView(NancyContext context, string viewName, object model = null) { var viewContext = new ViewLocationContext { Context = context }; return this.factory.RenderView(viewName, model, viewContext); }
// Filter out if the verb doesnt match. private bool MatchsVerbMapping(ViewLocationContext context, ViewLocation location) { var httpVerb = context.RequestContext.GetHttpMethod(); var verbs = location.Verbs; return verbs != null && !verbs.Any(verb => verb.Equals(httpVerb, StringComparison.OrdinalIgnoreCase)); }
private bool MatchesActionName(ViewLocationContext context, ViewLocation location) { return context.ActionDescriptor.ActionName.Equals(location.ActionName, StringComparison.OrdinalIgnoreCase); }
/// <summary> /// Gets a <see cref="IRenderContext"/> for the specified <see cref="ViewLocationContext"/>. /// </summary> /// <param name="viewLocationContext">The <see cref="ViewLocationContext"/> for which the context should be created.</param> /// <returns>A <see cref="IRenderContext"/> instance.</returns> public IRenderContext GetRenderContext(ViewLocationContext viewLocationContext) { return new DefaultRenderContext(this.viewResolver, this.viewCache, viewLocationContext); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRenderContext"/> class. /// </summary> /// <param name="viewResolver"></param> /// <param name="viewCache"></param> /// <param name="viewLocationContext"></param> public DefaultRenderContext(IViewResolver viewResolver, IViewCache viewCache, ViewLocationContext viewLocationContext) { this.viewResolver = viewResolver; this.viewCache = viewCache; this.viewLocationContext = viewLocationContext; }
/// <summary> /// Locates a view based on the provided information. /// </summary> /// <param name="viewName">The name of the view to locate.</param> /// <param name="model">The model that will be used with the view.</param> /// <param name="viewLocationContext">A <see cref="ViewLocationContext"/> instance, containing information about the context for which the view is being located.</param> /// <returns>A <see cref="ViewLocationResult"/> instance if the view could be found, otherwise <see langword="null"/>.</returns> public ViewLocationResult GetViewLocation(string viewName, dynamic model, ViewLocationContext viewLocationContext) { var fullName = string.Concat(viewName, ".sshtml"); var stream = GetBodyStream(fullName); return(GetViewLocationResult(fullName, stream)); }
public Nancy.Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { viewLocationContext.Context.Items["model"] = model; return(new HtmlResponse()); }