|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-Present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using React.Exceptions; |
| 12 | +using React.TinyIoC; |
| 13 | + |
| 14 | +#if NET451 |
| 15 | +using System.Web; |
| 16 | +using System.Web.Mvc; |
| 17 | +using HttpResponse = System.Web.HttpResponseBase; |
| 18 | +using IHtmlHelper = System.Web.Mvc.HtmlHelper; |
| 19 | +#else |
| 20 | +using Microsoft.AspNetCore.Mvc.Rendering; |
| 21 | +using IHtmlString = Microsoft.AspNetCore.Html.IHtmlContent; |
| 22 | +using HttpResponse = Microsoft.AspNetCore.Http.HttpResponse; |
| 23 | +using Microsoft.AspNetCore.Html; |
| 24 | +#endif |
| 25 | + |
| 26 | +namespace React.Router |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Render a React StaticRouter Component with context. |
| 30 | + /// </summary> |
| 31 | + public static class HtmlHelperExtensions |
| 32 | + { |
| 33 | + /// <summary> |
| 34 | + /// Gets the React environment |
| 35 | + /// </summary> |
| 36 | + private static IReactEnvironment Environment |
| 37 | + { |
| 38 | + get |
| 39 | + { |
| 40 | + return ReactEnvironment.GetCurrentOrThrow; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Render a React StaticRouter Component with context object. |
| 46 | + /// Can optionally be provided with a custom context handler to handle the various status codes. |
| 47 | + /// </summary> |
| 48 | + /// <param name="htmlHelper">MVC Razor <see cref="IHtmlHelper"/></param> |
| 49 | + /// <param name="componentName">Name of React Static Router component. Expose component globally to ReactJS.NET</param> |
| 50 | + /// <param name="props">Props to initialise the component with</param> |
| 51 | + /// <param name="path">F.x. from Request.Path. Used by React Static Router to determine context and routing.</param> |
| 52 | + /// <param name="contextHandler">Optional custom context handler, can be used instead of providing a Response object</param> |
| 53 | + /// <param name="htmlTag">HTML tag to wrap the component in. Defaults to <div></param> |
| 54 | + /// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param> |
| 55 | + /// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param> |
| 56 | + /// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param> |
| 57 | + /// <param name="containerClass">HTML class(es) to set on the container tag</param> |
| 58 | + /// <returns><see cref="IHtmlString"/> containing the rendered markup for provided React Router component</returns> |
| 59 | + public static IHtmlString ReactRouterWithContext<T>( |
| 60 | + this IHtmlHelper htmlHelper, |
| 61 | + string componentName, |
| 62 | + T props, |
| 63 | + string path = null, |
| 64 | + string htmlTag = null, |
| 65 | + string containerId = null, |
| 66 | + bool clientOnly = false, |
| 67 | + bool serverOnly = false, |
| 68 | + string containerClass = null, |
| 69 | + Action<HttpResponse, RoutingContext> contextHandler = null |
| 70 | + ) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + var response = htmlHelper.ViewContext.HttpContext.Response; |
| 75 | + path = path ?? htmlHelper.ViewContext.HttpContext.Request.Path; |
| 76 | + |
| 77 | + var reactComponent |
| 78 | + = Environment.CreateRouterComponent( |
| 79 | + componentName, |
| 80 | + props, |
| 81 | + path, |
| 82 | + containerId, |
| 83 | + clientOnly |
| 84 | + ); |
| 85 | + |
| 86 | + if (!string.IsNullOrEmpty(htmlTag)) |
| 87 | + { |
| 88 | + reactComponent.ContainerTag = htmlTag; |
| 89 | + } |
| 90 | + if (!string.IsNullOrEmpty(containerClass)) |
| 91 | + { |
| 92 | + reactComponent.ContainerClass = containerClass; |
| 93 | + } |
| 94 | + |
| 95 | + var executionResult = reactComponent.RenderRouterWithContext(clientOnly, serverOnly); |
| 96 | + |
| 97 | + if (executionResult.Context?.status != null) |
| 98 | + { |
| 99 | + // Use provided contextHandler |
| 100 | + if (contextHandler != null) |
| 101 | + { |
| 102 | + contextHandler(response, executionResult.Context); |
| 103 | + } |
| 104 | + // Handle routing context internally |
| 105 | + else |
| 106 | + { |
| 107 | + SetServerResponse.ModifyResponse(executionResult.Context, response); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return new HtmlString(executionResult.RenderResult); |
| 112 | + } |
| 113 | + finally |
| 114 | + { |
| 115 | + Environment.ReturnEngineToPool(); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments