public Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { // Intercept model viewLocationContext.Context.Items["###ViewModel###"] = model; return _defaultViewFactory.RenderView(viewName, model, 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) { if (string.IsNullOrEmpty(viewName)) { return null; } foreach (var convention in conventions) { var conventionBasedViewName = SafeInvokeConvention(convention, viewName, model, viewLocationContext); if (String.IsNullOrEmpty(conventionBasedViewName)) { continue; } var locatedView = this.viewLocator.LocateView(conventionBasedViewName); if (locatedView != null) { return locatedView; } } return null; }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRenderContext"/> class. /// </summary> /// <param name="viewResolver"></param> /// <param name="viewCache"></param> /// <param name="textResource"></param> /// <param name="viewLocationContext"></param> public DefaultRenderContext(IViewResolver viewResolver, IViewCache viewCache, ITextResource textResource, ViewLocationContext viewLocationContext) { this.viewResolver = viewResolver; this.viewCache = viewCache; this.textResource = textResource; this.viewLocationContext = viewLocationContext; this.textResourceFinder = new TextResourceFinder(textResource, viewLocationContext.Context); }
/// <summary> /// Renders the view and then call into the viewfactory /// that the TestingViewFactory is decorating /// </summary> /// <param name="viewName">The name of the view to render.</param> /// <param name="model">The module path of the module that is rendering the view.</param> /// <param name="viewLocationContext">A <see cref="ViewLocationContext"/> instance, containing information about the context for which the view is being rendered.</param> /// <returns>A response.</returns> public Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { // Intercept and store interesting stuff viewLocationContext.Context.Items[TestingViewContextKeys.VIEWMODEL] = model; viewLocationContext.Context.Items[TestingViewContextKeys.VIEWNAME] = viewName; viewLocationContext.Context.Items[TestingViewContextKeys.MODULENAME] = viewLocationContext.ModuleName; viewLocationContext.Context.Items[TestingViewContextKeys.MODULEPATH] = viewLocationContext.ModulePath; return this.decoratedViewFactory.RenderView(viewName, model, viewLocationContext); }
public DefaultViewFactoryFixture() { this.resolver = A.Fake<IViewResolver>(); this.renderContextFactory = A.Fake<IRenderContextFactory>(); this.viewLocationContext = new ViewLocationContext { Context = new NancyContext() }; }
private static string SafeInvokeConvention(Func<string, object, ViewLocationContext, string> convention, string viewName, dynamic model, ViewLocationContext viewLocationContext) { try { return convention.Invoke(viewName, model, viewLocationContext); } catch { return null; } }
public DefaultViewResolverFixture() { this.viewLocator = A.Fake<IViewLocator>(); this.viewResolver = new DefaultViewResolver(this.viewLocator, new ViewLocationConventions(Enumerable.Empty<Func<string, object, ViewLocationContext, string>>())); this.viewLocationContext = new ViewLocationContext { Context = new NancyContext() }; }
public DefaultViewFactoryFixture() { this.resolver = A.Fake<IViewResolver>(); this.renderContextFactory = A.Fake<IRenderContextFactory>(); this.conventions = new ViewLocationConventions(Enumerable.Empty<Func<string, object, ViewLocationContext, string>>()); this.viewLocationContext = new ViewLocationContext { Context = new NancyContext() }; }
public void Should_expose_context_from_viewlocationcontext() { // Given var nancyContext = new NancyContext(); var viewLocationContext = new ViewLocationContext { Context = nancyContext }; // When var context = new DefaultRenderContext(null, null, viewLocationContext); // Then context.Context.ShouldBeSameAs(nancyContext); }
public static Response RenderView(this IViewFactory factory, IViewLocationCache cache, NancyContext context, string viewName, object model = null) { var foundMatchingView = cache.Any(x => viewName.Equals(string.Concat(x.Location, "/", x.Name, ".", x.Extension), StringComparison.OrdinalIgnoreCase)); if (foundMatchingView) { var viewContext = new ViewLocationContext { Context = context }; context.Response = factory.RenderView(viewName, model, viewContext); } return context.Response; }
public Action<Stream> RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { if (model == null) model = new ExpandoObject(); _preRenderSubscribers .Where(x => x.Key == viewLocationContext.ModulePath) .Select(x => x.Value) .ToList() .ForEach(x => x.Invoke(viewName, model, viewLocationContext)); return _viewFactory.RenderView(viewName, model, viewLocationContext); }
public void Should_call_view_resolver_with_view_location_context_when_locating_view() { // Given var locationContext = new ViewLocationContext(); var resolver = A.Fake<IViewResolver>(); var context = new DefaultRenderContext(resolver, null, locationContext); // When context.LocateView(null, null); // Then) A.CallTo(() => resolver.GetViewLocation(A<string>.Ignored, A<object>.Ignored, locationContext)).MustHaveHappened(); }
/// <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) { if (!viewLocationContext.Context.Request.Path.StartsWith(SugarTown.UrlPrefix, StringComparison.OrdinalIgnoreCase)) { return _defaultResolver.GetViewLocation(viewName, model, viewLocationContext); } _renderer = new SugarTownViewRenderer(viewLocationContext.Context); var fullName = string.Concat(viewName, ".cshtml"); var stream = _renderer.GetBodyStream(fullName); return _renderer.GetViewLocationResult(fullName, stream); }
public DefaultViewFactoryFixture() { this.rootPathProvider = A.Fake<IRootPathProvider>(); A.CallTo(() => this.rootPathProvider.GetRootPath()).Returns("The root path"); this.resolver = A.Fake<IViewResolver>(); this.renderContextFactory = A.Fake<IRenderContextFactory>(); this.conventions = new ViewLocationConventions(Enumerable.Empty<Func<string, object, ViewLocationContext, string>>()); this.viewLocationContext = new ViewLocationContext { Context = new NancyContext() }; }
static string FunctionalAreaLocation( string viewName, object model, ViewLocationContext context, int level) { if (viewName.StartsWith("~/")) return viewName.Substring(2); var location = new List<string> {ROOT_MODULE}; var modulePathParts = context.ModulePath.Split( new[] {'/'}, StringSplitOptions.RemoveEmptyEntries); if (modulePathParts.Length - level > 0) { location.AddRange(modulePathParts.Skip(level)); } location.Add("_views"); location.Add(viewName); return string.Join("/", location); }
/// <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) { if (string.IsNullOrEmpty(viewName)) { return null; } if (viewLocationContext == null) { return null; } viewLocationContext.Context.Trace.TraceLog.WriteLog(x => x.AppendLine(string.Concat("[DefaultViewResolver] Resolving view for '", viewName , "', using view location conventions."))); foreach (var convention in conventions) { var conventionBasedViewName = SafeInvokeConvention(convention, viewName, model, viewLocationContext); if (string.IsNullOrEmpty(conventionBasedViewName)) { continue; } viewLocationContext.Context.Trace.TraceLog.WriteLog(x => x.AppendLine(string.Concat("[DefaultViewResolver] Attempting to locate view using convention '", conventionBasedViewName, "'"))); var locatedView = this.viewLocator.LocateView(conventionBasedViewName, viewLocationContext.Context); if (locatedView != null) { viewLocationContext.Context.Trace.TraceLog.WriteLog(x => x.AppendLine(string.Concat("[DefaultViewResolver] View resolved at '", conventionBasedViewName, "'"))); return locatedView; } } viewLocationContext.Context.Trace.TraceLog.WriteLog(x => x.AppendLine("[DefaultViewResolver] No view could be resolved using the available view location conventions.")); return null; }
/// <summary> /// Renders the view with the name and model defined by the <paramref name="viewName"/> and <paramref name="model"/> parameters. /// </summary> /// <param name="viewName">The name of the view to render.</param> /// <param name="model">The model that should be passed into the view.</param> /// <param name="viewLocationContext">A <see cref="ViewLocationContext"/> instance, containing information about the context for which the view is being rendered.</param> /// <returns>A delegate that can be invoked with the <see cref="Stream"/> that the view should be rendered to.</returns> public Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { if (viewName == null && model == null) { throw new ArgumentException("View name and model parameters cannot both be null."); } if (model == null && viewName.Length == 0) { throw new ArgumentException("The view name parameter cannot be empty when the model parameters is null."); } if (viewLocationContext == null) { throw new ArgumentNullException("viewLocationContext", "The value of the viewLocationContext parameter cannot be null."); } var actualViewName = viewName ?? GetViewNameFromModel(model); return this.GetRenderedView(actualViewName, model, viewLocationContext); }
public void PreloadRazorView(string viewPath, object viewModel) { // It seems the only way to precompile a view in Nancy is to actually use it? using (NancyContext context = GetDummyContext()) { ViewLocationResult viewLocation = _viewLocator.LocateView(viewPath, context); ViewLocationContext locationContext = new ViewLocationContext() { Context = context }; IRenderContext renderContext = _renderContextFactory.GetRenderContext(locationContext); using (Response renderedView = _engine.RenderView(viewLocation, viewModel, renderContext, isPartial: true)) using (MemoryStream stream = new MemoryStream()) { renderedView.Contents(stream); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { // Disable Mono warning about renderedHtml not being used. It's useful to see it when debugging. #pragma warning disable 219 string renderedHtml = reader.ReadToEnd(); #pragma warning restore 219 } } } }
private Response GetRenderedView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { var viewLocationResult = this.viewResolver.GetViewLocation(viewName, model, viewLocationContext); var resolvedViewEngine = GetViewEngine(viewLocationResult, viewLocationContext.Context); if (resolvedViewEngine == null) { viewLocationContext.Context.Trace.TraceLog.WriteLog(x => x.AppendLine("[DefaultViewFactory] Unable to find view engine that could render the view.")); throw new ViewNotFoundException(viewName, this.viewEngineExtensions, this.GetInspectedLocations(viewName, model, viewLocationContext), this.rootPathProvider); } viewLocationContext.Context.Trace.TraceLog.WriteLog(x => x.AppendLine(string.Concat("[DefaultViewFactory] Rendering view with view engine ", resolvedViewEngine.GetType().FullName))); return SafeInvokeViewEngine( resolvedViewEngine, viewLocationResult, GetSafeModel(model), this.renderContextFactory.GetRenderContext(viewLocationContext) ); }
/// <summary> /// Renders the view with the name and model defined by the <paramref name="viewName"/> and <paramref name="model"/> parameters. /// </summary> /// <param name="viewName">The name of the view to render.</param> /// <param name="model">The model that should be passed into the view.</param> /// <param name="viewLocationContext">A <see cref="ViewLocationContext"/> instance, containing information about the context for which the view is being rendered.</param> /// <returns>A delegate that can be invoked with the <see cref="Stream"/> that the view should be rendered to.</returns> public Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { if (viewName == null && model == null) { throw new ArgumentException("View name and model parameters cannot both be null."); } if (model == null && viewName.Length == 0) { throw new ArgumentException("The view name parameter cannot be empty when the model parameters is null."); } if (viewLocationContext == null) { throw new ArgumentNullException("viewLocationContext", "The value of the viewLocationContext parameter cannot be null."); } var actualViewName = viewName ?? GetViewNameFromModel(model, viewLocationContext.Context); viewLocationContext.Context.Trace.TraceLog.WriteLog(x => x.AppendLine(string.Concat("[DefaultViewFactory] Rendering view with name ", actualViewName))); return this.GetRenderedView(actualViewName, model, viewLocationContext); }
private string[] GetInspectedLocations(string viewName, dynamic model, ViewLocationContext viewLocationContext) { var inspectedLocations = new List<string>(); foreach (var convention in conventions) { try { var location = convention.Invoke(viewName, model, viewLocationContext); if (!string.IsNullOrWhiteSpace(location)) { inspectedLocations.Add(location); } } catch { } } return inspectedLocations.ToArray(); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultRenderContext"/> class. /// </summary> /// <param name="viewResolver"></param> /// <param name="viewCache"></param> /// <param name="textResource"></param> /// <param name="viewLocationContext"></param> public DefaultRenderContext(IViewResolver viewResolver, IViewCache viewCache, ITextResource textResource, ViewLocationContext viewLocationContext) { this.viewResolver = viewResolver; this.viewCache = viewCache; this.textResource = textResource; this.viewLocationContext = viewLocationContext; this.textResourceFinder = new TextResourceFinder(textResource, viewLocationContext.Context); }
/// <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; }
public void Should_retrieve_view_from_view_locator_using_provided_module_path() { // Given var factory = this.CreateFactory(); var model = new object(); var viewContext = new ViewLocationContext { Context = new NancyContext(), ModulePath = "/bar" }; // When Record.Exception(() => factory.RenderView(null, model, viewContext)); // Then A.CallTo(() => this.resolver.GetViewLocation(A<string>.Ignored, A<object>.Ignored, A<ViewLocationContext>.That.Matches(x => x.ModulePath.Equals("/bar")))).MustHaveHappened(); }
/// <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, this.textResource, viewLocationContext)); }
private static string SafeInvokeConvention(Func <string, object, ViewLocationContext, string> convention, string viewName, dynamic model, ViewLocationContext viewLocationContext) { try { return(convention.Invoke(viewName, model, viewLocationContext)); } catch { return(null); } }
public void Should_return_same_path_when_parsing_path_if_path_doesnt_contain_tilde() { const string input = "/scripts/test.js"; var url = new Url { BasePath = "/base/path", Path = "/" }; var request = new Request("GET", url); var nancyContext = new NancyContext { Request = request }; var viewLocationContext = new ViewLocationContext { Context = nancyContext }; var context = new DefaultRenderContext(null, null, viewLocationContext); var result = context.ParsePath(input); result.ShouldEqual(input); }
public void Should_throw_if_context_does_not_contain_valid_csrf_token_and_its_requested() { var nancyContext = new NancyContext(); var viewLocationContext = new ViewLocationContext { Context = nancyContext }; var context = new DefaultRenderContext(null, null, null, viewLocationContext); var result = Record.Exception(() => context.GetCsrfToken()); result.ShouldBeOfType(typeof(InvalidOperationException)); }
private Response GetRenderedView(string viewName, dynamic model, ViewLocationContext viewLocationContext) { var viewLocationResult = this.viewResolver.GetViewLocation(viewName, model, viewLocationContext); var resolvedViewEngine = GetViewEngine(viewLocationResult); if (resolvedViewEngine == null) { throw new ViewNotFoundException(viewName, this.viewEngineExtensions); } return SafeInvokeViewEngine( resolvedViewEngine, viewLocationResult, GetSafeModel(model), this.renderContextFactory.GetRenderContext(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 void Should_return_csrf_token_from_context_if_it_exists() { var nancyContext = new NancyContext(); nancyContext.Items[CsrfToken.DEFAULT_CSRF_KEY] = "testing"; var viewLocationContext = new ViewLocationContext { Context = nancyContext }; var context = new DefaultRenderContext(null, null, null, viewLocationContext); var result = context.GetCsrfToken(); result.ShouldNotBeNull(); result.Key.ShouldEqual(CsrfToken.DEFAULT_CSRF_KEY); result.Value.ShouldEqual("testing"); }
public void Should_replace_tilde_with_base_path_when_parsing_path_if_one_present() { const string input = "~/scripts/test.js"; var url = new Url { BasePath = "/base/path/", Path = "/" }; var request = new Request("GET", url); var nancyContext = new NancyContext { Request = request }; var viewLocationContext = new ViewLocationContext { Context = nancyContext }; var context = new DefaultRenderContext(null, null, viewLocationContext); var result = context.ParsePath(input); result.ShouldEqual("/base/path/scripts/test.js"); }
/// <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, this.textResource, viewLocationContext); }
private ViewLocationContext GetContext() { var context = new ViewLocationContext(); context.Context = new NancyContext(); return context; }