private PropertySerializeInfo GetPropertySerializationInfo(DotvvmProperty property) { var binding = GetBinding(property); if (binding == null && !typeof(ITemplate).IsAssignableFrom(property.PropertyType)) { JsonSerializerSettings settings = DefaultViewModelSerializer.CreateDefaultSettings(); settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml; return(new PropertySerializeInfo { Property = property, Js = JsonConvert.SerializeObject(GetValue(property), Formatting.None, settings), IsSerializable = true }); } else if (binding is IValueBinding) { return(new PropertySerializeInfo { Property = property, Js = (binding as IValueBinding).GetKnockoutBindingExpression(this), IsSerializable = true }); } else { return(new PropertySerializeInfo { Property = property }); } }
public void TestInit() { configuration = DotvvmConfiguration.CreateDefault(); configuration.Security.SigningKey = Convert.FromBase64String("Uiq1FXs016lC6QaWIREB7H2P/sn4WrxkvFkqaIKpB27E7RPuMipsORgSgnT+zJmUu8zXNSJ4BdL73JEMRDiF6A1ScRNwGyDxDAVL3nkpNlGrSoLNM1xHnVzSbocLFDrdEiZD2e3uKujguycvWSNxYzjgMjXNsaqvCtMu/qRaEGc="); configuration.Security.EncryptionKey = Convert.FromBase64String("jNS9I3ZcxzsUSPYJSwzCOm/DEyKFNlBmDGo9wQ6nxKg="); serializer = new DefaultViewModelSerializer(new DefaultViewModelProtector()); context = new DotvvmRequestContext() { Configuration = configuration, OwinContext = new Microsoft.Owin.Fakes.StubIOwinContext() { RequestGet = () => new Microsoft.Owin.Fakes.StubIOwinRequest() { UriGet = () => new Uri("http://localhost:8628/Sample1"), UserGet = () => new WindowsPrincipal(WindowsIdentity.GetAnonymous()), HeadersGet = () => new HeaderDictionary(new Dictionary <string, string[]>()), EnvironmentGet = () => new Dictionary <string, object>() { { "owin.RequestPathBase", "" } } } }, ResourceManager = new ResourceManager(configuration), Presenter = configuration.RouteTable.GetDefaultPresenter() }; }
private PropertySerializeInfo GetPropertySerializationInfo(DotvvmProperty property) { if (ContainsPropertyStaticValue(property)) { JsonSerializerSettings settings = DefaultViewModelSerializer.CreateDefaultSettings(); settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml; return(new PropertySerializeInfo { Property = property, Js = JsonConvert.SerializeObject(GetValue(property), Formatting.None, settings), IsSerializable = true }); } else if (GetBinding(property) is IValueBinding valueBinding) { return(new PropertySerializeInfo { Property = property, Js = valueBinding.GetKnockoutBindingExpression(this), IsSerializable = true }); } else { return(new PropertySerializeInfo { Property = property }); } }
public void TestInit() { configuration = DotvvmConfiguration.CreateDefault(services => { services.AddSingleton <IDataProtectionProvider>(new DpapiDataProtectionProvider("DotVVM Tests")); services.AddTransient <IViewModelProtector, DefaultViewModelProtector>(); services.AddTransient <ICsrfProtector, DefaultCsrfProtector>(); services.AddSingleton <ICookieManager, ChunkingCookieManager>(); }); configuration.Security.SigningKey = Convert.FromBase64String("Uiq1FXs016lC6QaWIREB7H2P/sn4WrxkvFkqaIKpB27E7RPuMipsORgSgnT+zJmUu8zXNSJ4BdL73JEMRDiF6A1ScRNwGyDxDAVL3nkpNlGrSoLNM1xHnVzSbocLFDrdEiZD2e3uKujguycvWSNxYzjgMjXNsaqvCtMu/qRaEGc="); configuration.Security.EncryptionKey = Convert.FromBase64String("jNS9I3ZcxzsUSPYJSwzCOm/DEyKFNlBmDGo9wQ6nxKg="); var requestMock = new Mock <IHttpRequest>(); requestMock.SetupGet(m => m.Url).Returns(new Uri("http://localhost:8628/Sample1")); requestMock.SetupGet(m => m.Path).Returns(new DotvvmHttpPathString(new PathString("/Sample1"))); requestMock.SetupGet(m => m.PathBase).Returns(new DotvvmHttpPathString(new PathString(""))); requestMock.SetupGet(m => m.Method).Returns("GET"); requestMock.SetupGet(m => m.Headers).Returns(new DotvvmHeaderCollection(new HeaderDictionary(new Dictionary <string, string[]>()))); var contextMock = new Mock <IHttpContext>(); contextMock.SetupGet(m => m.Request).Returns(requestMock.Object); contextMock.SetupGet(m => m.User).Returns(new WindowsPrincipal(WindowsIdentity.GetAnonymous())); serializer = configuration.ServiceLocator.GetService <IViewModelSerializer>() as DefaultViewModelSerializer; context = new DotvvmRequestContext(contextMock.Object, configuration, configuration.ServiceProvider) { Presenter = configuration.RouteTable.GetDefaultPresenter(configuration.ServiceProvider), }; }
public void TestInit() { configuration = DotvvmConfiguration.CreateDefault(); configuration.ServiceLocator.RegisterSingleton <IDataProtectionProvider>(() => new DpapiDataProtectionProvider("dotvvm test")); configuration.Security.SigningKey = Convert.FromBase64String("Uiq1FXs016lC6QaWIREB7H2P/sn4WrxkvFkqaIKpB27E7RPuMipsORgSgnT+zJmUu8zXNSJ4BdL73JEMRDiF6A1ScRNwGyDxDAVL3nkpNlGrSoLNM1xHnVzSbocLFDrdEiZD2e3uKujguycvWSNxYzjgMjXNsaqvCtMu/qRaEGc="); configuration.Security.EncryptionKey = Convert.FromBase64String("jNS9I3ZcxzsUSPYJSwzCOm/DEyKFNlBmDGo9wQ6nxKg="); var requestMock = new Mock <IOwinRequest>(); requestMock.SetupGet(m => m.Uri).Returns(new Uri("http://localhost:8628/Sample1")); requestMock.SetupGet(m => m.User).Returns(new WindowsPrincipal(WindowsIdentity.GetAnonymous())); requestMock.SetupGet(m => m.Headers).Returns(new HeaderDictionary(new Dictionary <string, string[]>())); requestMock.SetupGet(m => m.Environment).Returns(new Dictionary <string, object>() { { "owin.RequestPathBase", "" } }); var contextMock = new Mock <IOwinContext>(); contextMock.SetupGet(m => m.Request).Returns(requestMock.Object); serializer = new DefaultViewModelSerializer(configuration); context = new DotvvmRequestContext() { Configuration = configuration, OwinContext = contextMock.Object, ResourceManager = new ResourceManager(configuration), Presenter = configuration.RouteTable.GetDefaultPresenter(), ViewModelSerializer = serializer }; }
public void TestInit() { configuration = DotvvmConfiguration.CreateDefault(); configuration.ServiceLocator.RegisterSingleton<IDataProtectionProvider>(() => new DpapiDataProtectionProvider("dotvvm test")); configuration.Security.SigningKey = Convert.FromBase64String("Uiq1FXs016lC6QaWIREB7H2P/sn4WrxkvFkqaIKpB27E7RPuMipsORgSgnT+zJmUu8zXNSJ4BdL73JEMRDiF6A1ScRNwGyDxDAVL3nkpNlGrSoLNM1xHnVzSbocLFDrdEiZD2e3uKujguycvWSNxYzjgMjXNsaqvCtMu/qRaEGc="); configuration.Security.EncryptionKey = Convert.FromBase64String("jNS9I3ZcxzsUSPYJSwzCOm/DEyKFNlBmDGo9wQ6nxKg="); serializer = new DefaultViewModelSerializer(configuration); context = new DotvvmRequestContext() { Configuration = configuration, OwinContext = new Microsoft.Owin.Fakes.StubIOwinContext() { RequestGet = () => new Microsoft.Owin.Fakes.StubIOwinRequest() { UriGet = () => new Uri("http://localhost:8628/Sample1"), UserGet = () => new WindowsPrincipal(WindowsIdentity.GetAnonymous()), HeadersGet = () => new HeaderDictionary(new Dictionary<string, string[]>()), EnvironmentGet = () => new Dictionary<string, object>() { { "owin.RequestPathBase", "" } } } }, ResourceManager = new ResourceManager(configuration), Presenter = configuration.RouteTable.GetDefaultPresenter(), ViewModelSerializer = serializer }; }
static T Populate <T>(string json, JObject encryptedValues = null) { var serializer = JsonSerializer.Create(DefaultViewModelSerializer.CreateDefaultSettings()); serializer.Converters.Add(CreateConverter(true, encryptedValues)); //serializer.Populate(new StringReader(json), viewModel); return(serializer.Deserialize <T>(new JsonTextReader(new StringReader(json)))); }
/// <summary> /// Returns Javascript expression that represents the property value (even if the property contains hardcoded value) /// </summary> public static string GetKnockoutBindingExpression(this DotvvmBindableObject obj, DotvvmProperty property) { var binding = obj.GetValueBinding(property); if (binding != null) { return(binding.GetKnockoutBindingExpression(obj)); } return(JsonConvert.SerializeObject(obj.GetValue(property), DefaultViewModelSerializer.CreateDefaultSettings())); }
static string Serialize <T>(T viewModel, out JObject encryptedValues, bool isPostback = false) { encryptedValues = new JObject(); var serializer = JsonSerializer.Create(DefaultViewModelSerializer.CreateDefaultSettings()); serializer.Converters.Add(CreateConverter(isPostback, encryptedValues)); var output = new StringWriter(); serializer.Serialize(output, viewModel); return(output.ToString()); }
protected internal string TranslateValueOrBinding(DotvvmProperty property) { var binding = GetValueBinding(property); if (binding == null) { return(JsonConvert.SerializeObject(GetValue(property), DefaultViewModelSerializer.CreateDefaultSettings())); } else { return("ko.unwrap(" + binding.GetKnockoutBindingExpression(this) + ")"); } }
/// <summary> /// Renders the redirect response. /// </summary> public static void SetRedirectResponse(IOwinContext OwinContext, string url, int statusCode) { if (!DotvvmPresenter.DeterminePartialRendering(OwinContext)) { OwinContext.Response.Headers["Location"] = url; OwinContext.Response.StatusCode = statusCode; } else { OwinContext.Response.StatusCode = 200; OwinContext.Response.ContentType = "application/json"; OwinContext.Response.Write(DefaultViewModelSerializer.GenerateRedirectActionResponse(url)); } }
public void WriteRedirectReponse(IHttpContext httpContext, string url, int statusCode = (int)HttpStatusCode.Redirect, bool replaceInHistory = false, bool allowSpaRedirect = false) { if (!DotvvmPresenter.DeterminePartialRendering(httpContext)) { httpContext.Response.Headers["Location"] = url; httpContext.Response.StatusCode = statusCode; } else { httpContext.Response.StatusCode = 200; httpContext.Response.ContentType = "application/json"; httpContext.Response.Write(DefaultViewModelSerializer.GenerateRedirectActionResponse(url, replaceInHistory, allowSpaRedirect)); } }
public SerializerTests(bool allowDiffs) { this.allowDiffs = allowDiffs; this.configuration = DotvvmConfiguration.CreateDefault(s => { s.AddSingleton <IViewModelProtector, FakeProtector>(); }); this.serializer = configuration.ServiceProvider.GetRequiredService <IViewModelSerializer>() as DefaultViewModelSerializer; this.viewModel = CreateViewModel(); this.request = new TestDotvvmRequestContext() { ViewModel = viewModel, CsrfToken = "", ResourceManager = new ResourceManagement.ResourceManager(configuration.Resources), Configuration = configuration, ModelState = new ModelState() }; }
private static string TranslateRouteParameter(DotvvmBindableObject control, KeyValuePair <string, object> param, bool caseSensitive = false) { string expression = ""; if (param.Value is IBinding) { EnsureValidBindingType(param.Value as IBinding); expression = (param.Value as IValueBinding)?.GetKnockoutBindingExpression(control) ?? JsonConvert.SerializeObject((param.Value as IStaticValueBinding)?.Evaluate(control), DefaultViewModelSerializer.CreateDefaultSettings()); } else { expression = JsonConvert.SerializeObject(param.Value, DefaultViewModelSerializer.CreateDefaultSettings()); } return(JsonConvert.SerializeObject(caseSensitive ? param.Key : param.Key.ToLower()) + ": " + expression); }
public void WriteRedirectResponse(IHttpContext httpContext, string url, int statusCode = (int)HttpStatusCode.Redirect, bool replaceInHistory = false, bool allowSpaRedirect = false) { if (!DotvvmPresenter.DeterminePartialRendering(httpContext)) { httpContext.Response.Headers["Location"] = url; httpContext.Response.StatusCode = statusCode; } else { httpContext.Response.StatusCode = 200; httpContext.Response.ContentType = "application/json"; httpContext.Response .WriteAsync(DefaultViewModelSerializer.GenerateRedirectActionResponse(url, replaceInHistory, allowSpaRedirect)) .GetAwaiter().GetResult(); // ^ we just wait for this Task. Redirect API never was async and the response size is small enough that we can't quite safely wait for the result // .GetAwaiter().GetResult() preserves stack traces across async calls, thus I like it more that .Wait() } }
/// <summary> /// Renders the redirect response. /// </summary> /// <param name="forceRefresh"></param> public static void SetRedirectResponse(IOwinContext owinContext, string url, int statusCode, bool replaceInHistory = false, bool allowSpaRedirect = false) { if (!DotvvmPresenter.DeterminePartialRendering(owinContext)) { owinContext.Response.Headers["Location"] = url; owinContext.Response.StatusCode = statusCode; } else { //if (DotvvmPresenter.DetermineIsPostBack(owinContext) && DotvvmPresenter.DetermineSpaRequest(owinContext) && !url.Contains("//")) //{ // // if we are in SPA postback, redirect should point at #! URL // url = "#!" + url; //} owinContext.Response.StatusCode = 200; owinContext.Response.ContentType = "application/json"; owinContext.Response.Write(DefaultViewModelSerializer.GenerateRedirectActionResponse(url, replaceInHistory, allowSpaRedirect)); } }
private PropertySerializeInfo GetPropertySerializationInfo(DotvvmProperty property) { var binding = GetBinding(property); if (binding == null) { return(new PropertySerializeInfo { Property = property, Js = JsonConvert.SerializeObject(GetValue(property), Formatting.None, DefaultViewModelSerializer.CreateDefaultSettings()), IsSerializable = true }); } else if (binding is IValueBinding) { return(new PropertySerializeInfo { Property = property, Js = (binding as IValueBinding).GetKnockoutBindingExpression(this), IsSerializable = true }); } else { return(new PropertySerializeInfo { Property = property }); } }
public DiagnosticsViewModelSerializerDecorator(DefaultViewModelSerializer defaultViewModelSerializer, DiagnosticsDataStore diagStore) { this.defaultViewModelSerializer = defaultViewModelSerializer; this.diagStore = diagStore; }
private static void AddValidatedValue(IHtmlWriter writer, IDotvvmRequestContext context, DotvvmProperty prop, DotvvmControl control) { writer.AddKnockoutDataBind("dotvvmValidation", control, ValueProperty, renderEvenInServerRenderingMode: true); // render options var bindingGroup = new KnockoutBindingGroup(); foreach (var property in ValidationOptionProperties) { var javascriptName = KnockoutHelper.ConvertToCamelCase(property.Name); var optionValue = control.GetValue(property); if (!object.Equals(optionValue, property.DefaultValue)) { bindingGroup.Add(javascriptName, JsonConvert.SerializeObject(optionValue, DefaultViewModelSerializer.CreateDefaultSettings())); } } writer.AddKnockoutDataBind("dotvvmValidationOptions", bindingGroup); }
internal static Task SetCachedViewModelMissingResponse(this IDotvvmRequestContext context) { context.HttpContext.Response.StatusCode = 200; context.HttpContext.Response.ContentType = "application/json"; return(context.HttpContext.Response.WriteAsync(DefaultViewModelSerializer.GenerateMissingCachedViewModelResponse())); }
public virtual void Add(string name, DotvvmControl control, DotvvmProperty property, Action nullBindingAction = null) { var binding = control.GetValueBinding(property); if (binding == null) { if (nullBindingAction != null) { nullBindingAction(); } else { Add(name, JsonConvert.SerializeObject(control.GetValue(property), DefaultViewModelSerializer.CreateDefaultSettings())); } } else { entries.Add(new KnockoutBindingInfo() { Name = name, Expression = GetKnockoutBindingExpression(control, control.GetValueBinding(property)) }); } }