Solibulo

Uninteresting things

UpdatePanel html helper for jQuery/ASP.NET MVC

by softlion 9. July 2010 17:36

Sometimes you don’t have time to write javascript code, and if like me you don’t want to use microsoft ajax, you’ll end up creating a basic form.

 

It would be nice to have the equivalent of Ajax.BeginForm( ) but using the jQuery library.

Well now you have it.

 

 

Usage

 

Add the jquery ajaxform plugin to your site

See http://jquery.malsup.com/form/

 

In a partial control (called DemoAjaxForm here), add your form

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DemoFormModel>" %> <%@ Import Namespace="Mvc.Extensions" %> <% using(Html.UpdatePanel( "demoFormPost","home" ) ) { %> first name : <%: Html.EditorFor(m => m.FirstName) %> <%: Html.ValidationMessageFor(m => m.FirstName) %><br /> friend first name : <%: Html.EditorFor(m => m.FirstNameOfFriend) %> <%: Html.ValidationMessageFor(m => m.FirstNameOfFriend)%><br /> <br /> <% if(Model.PercentMatch.HasValue) { %> <div style="font-weight: bold; text-align: center; font-size: x-large">LOVE MATCH <%: Model.PercentMatch %>%</div> <br /> <% } %> <div><%: Html.ValidationSummary(true) %></div> <input type="submit" style="border: 1px solid #333" /> <% } %>

 

 

 

 

 

On your home controller, add this demoFormPost action

you can mark this action with [HttpPost] (you should)

 

 

        public ActionResult DemoFormPost(DemoFormModel model)
        {
            if (ModelState.IsValid)
            {
                model.FirstName.Trim();
                model.FirstNameOfFriend.Trim();
                model.CalcLoveMatch();
            }
            else
                ModelState.AddModelError("", "Fill all fields mark with a star");

            return PartialView("DemoAjaxForm", model);
        }

 

The model

    public class DemoFormModel
    {
        [Required(ErrorMessage="*")]
        [StringLength(20, ErrorMessage = "Max 20 caractères")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "*")]
        [StringLength(20, ErrorMessage = "Max 20 caractères")]
        public string FirstNameOfFriend { get; set; }

        public int? PercentMatch;


        public void CalcLoveMatch()
        {
            PercentMatch = new Random().Next(0,100);
        }

    }

 

Full code of the UpdatePanel Html helper

 

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace Mvc.Extensions {

public static partial class HtmlExtensions { public static MvcUpdatePanel UpdatePanel(this HtmlHelper html, string action, string controller,

object routeValues = null, object htmlAttributes = null) { return new MvcUpdatePanel(html, action, controller, routeValues, htmlAttributes); } } public class MvcUpdatePanel : IDisposable { readonly MvcForm _form; readonly TextWriter _writer; bool _disposed; public MvcUpdatePanel(HtmlHelper html, string action, string controller, object routeValues,

object htmlAttributes) { _writer = html.ViewContext.Writer; RouteValueDictionary dicRouteValues = new RouteValueDictionary(routeValues); IDictionary<string,object> dicHtmlAttributes = new RouteValueDictionary(htmlAttributes); string formId; object oFormId; if (dicHtmlAttributes.TryGetValue("id", out oFormId)) formId = oFormId as string; else { formId = GenerateFormId(); dicHtmlAttributes.Add("id", formId); } //Render scripts RenderScripts(formId); //Render form _form = html.BeginForm(action, controller, dicRouteValues, FormMethod.Post, dicHtmlAttributes); } #region private private static string GenerateFormId() { return "mup" + new Random().Next(0, 1000000); } private void RenderScripts(string formId) { var tagBuilder = new TagBuilder("div"); tagBuilder.MergeAttribute("id", formId+"div"); _writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); tagBuilder = new TagBuilder("script"); tagBuilder.MergeAttribute("type", "text/javascript"); _writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); const string script = @" $(function(){{ $('#{0}').ajaxForm({{ target: '#{1}' }}); }});"; _writer.Write(String.Format(CultureInfo.InvariantCulture, script, formId, formId + "div")); _writer.Write(tagBuilder.ToString(TagRenderMode.EndTag)); } #endregion #region IDisposable Members /// <summary> /// Render end of form /// </summary> public void Dispose() { Dispose(true /* disposing */); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { _disposed = true; _form.Dispose(); _writer.Write("</div>"); } } #endregion }

}

Tags:

First bad design in ASP.NET MVC: form handling

by softlion 23. March 2010 08:25

Imagine you have this View, Model and this Action:

 

<% using (Html.BeginForm("MyActionPost")) { %›
         <%= Html.TextBoxFor(m => m.Price)%> <input type="submit" value="Save" />
 ‹% } %›
 

 

[HttpPost]
public ActionResult ProductEditSave(ProductModel product)
{
    product.PublicPrice += 5m;
    return View("ProductEditForm", product);
}

 

public class ProductModel { public int Price { get; set; } }

  

After a fist submit You'll expect to see "5" in the box, after a second you'll expect to see "10". Instead you'll always see "0".

 

Why ? This is by "design" in ASP.NET MVC ! The "ModelState" collection content, which contains originally posted values, takes precedence over the values passed in the Model object (product).

 

Reference and possible workarounds :
http://stackoverflow.com/questions/594600/possible-bug-in-asp-net-mvc-with-form-values-being-replaced/2498155#2498155

 

NSSM, a free “modern” alternative to srvany+instsrv

by softlion 28. December 2009 11:32

 

 

When you want to run a program as a service on Windows, the program have to be created specifically for this by implementing the Windows service interface.

 

When you don’t want to waste your time, or for any reason you could think of, there is a solution to run an exe as service. You can use the well known solution combining instsrv+srvany which works only on 32 bit windows, or NSSM which now works both on Win32 and x64.

 

Usage: type NSSM without argument:

 

image

 

Practical usage

 

For use in a single install.bat batch file without any user interaction:

 

rem stop and uninstall the service. Useful if the .bat is run twice.
sc stop YourServiceName
nssm remove YourServiceName confirm


rem install and configure the service
nssm install YourServiceName “%CD%\Your Service Name.exe” 
sc config YourServiceName start= auto depend= tcpip/LmHosts/Eventlog
sc description YourServiceName "Description of your service for display in service manager"
sc start YourServiceName 

 

In .bat files, %CD% is replaced with the current path.

 

Our service is configured to start at boot (start= auto), so we need to specify which service should be up before this service can start (depend= …)

 

Replace nssm by nssm-x64 if you run on 64 bits Windows.

 

Download

 

Download File - NSSM-2.1

 

References

 

Original NSSM (2006)

 

 

___

Tags:

General

How To: modify the default template of "Create New Stored Procedure" in SQL Server Management Studio

by softlion 1. December 2009 14:44

 

Simply modify the file “Create Stored Procedure (New Menu).sql” in:

 

C:\Program Files\Microsoft SQL Server\(version: 90, 100, …)\Tools\Binn\VSShell\Common7\IDE\SqlWorkbenchProjectItems\Sql\Stored Procedure

Tags:

sql

How to fix a bug in an obfuscated third party DLL

by softlion 1. December 2009 12:55

Recently a customer wanted me to evaluate the cost and fix all XSS holes in their website. Their security specialist found some holes using an obscure software only security specialists have and gave me his report and a list of webpage where it occurs.

After some digging I found there was one main hole - the same and on every page of their website. The hole was coming from the code of an UI component, a four years old version of the Telerik UI library for ASP.NET where XSS was not in the light.

 

My first report was to replace this component everywhere. As it would cost too much to the company, I was aimed to propose another solution : decompile the faulty assembly, find and fix the bug, and recompile it. It seemed easy. Helas ! This commercial library is obfuscated by Xenocode (a commercial obfuscator) which removes a lot of code unused by the CLR but necessary to the recompilation. So it was not an option.

 

THE SOLUTION

 

Then I came with an idea from my childhood where I was playing with raw asm and modified a tetris software because the blocs were not rotating in the correct direction …

Why not replace the faulting method pointer with the pointer of a fixed method dynamically in memory ? A lot of things run against this idea.One of them is that the code memory area is protected since Windows XP. And code injection does not work on 64 bit platforms at all.

Then I googled and found the graal… A post by Ziad Elmalki called “CLR Injection: Runtime Method Replacer”.

 

With a few lines of code I make it work with the web project.

I created a class “Demo.Fix” with a method InjectFix to be called in global.axax’s Application_Start event.

The first thing this method does is to check wether the assembly containing the code to fix is already loaded. If it is not it handles the AppDomain’s AssemblyLoad event.

 

using System; using System.Reflection; using System.Web; using System.Linq; using NativeAssemblerInjection; namespace Demo { public class Fix { /// <summary> /// Dynamically replace the failing method with our /// </summary> /// <remarks> /// First search if the assembly containing the class and method to replace is already /// loaded. If it is not already loaded, handle the AssemblyLoad event on the /// application domain. /// </remarks> public static void InjectFix() { var assembly = (from a in AppDomain.CurrentDomain.GetAssemblies() where a.FullName.Contains("FaultingAssemblyName") select a).FirstOrDefault(); if (assembly != null) DoReplace(assembly); else AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad; } /// <summary> /// Wait for the assembly containing the class and method to replace to be loaded. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> static void CurrentDomain_AssemblyLoad(object sender, System.AssemblyLoadEventArgs args) { if (args.LoadedAssembly.FullName.Contains("FaultingAssemblyName")) { DoReplace(args.LoadedAssembly); AppDomain.CurrentDomain.AssemblyLoad -= CurrentDomain_AssemblyLoad; } } /// <summary> /// Dynamically replace the failing method in <paramref name="assembly"/> by our /// own method. /// </summary> /// <param name="assembly">assembly containing the failing method</param> private static void DoReplace(Assembly assembly) { MethodBase oldMethod = assembly.GetType("FullNamespace.WebControls.x7fbe3d3b15648174")

.GetMethod("xe07c485b9dc80480", BindingFlags.NonPublic | BindingFlags.Instance); MethodBase newMethod = typeof(Fix).GetMethod("ReplacementMethod", BindingFlags.NonPublic | BindingFlags.Instance); MethodUtil.ReplaceMethod(newMethod, oldMethod); } /// <summary> /// Replacement method which corrects the behaviour of the original method /// </summary> /// <remarks> /// The signature must be the exact same as the original method. When called, the context /// (this) is the one from the original object, not the one from this class. /// </remarks> private string ReplacementMethod() { ... } } }

And it works both in 32 and 64 bits … amazing.

 

References

CLR Injection: Runtime Method Replacer

Tags:

Easy Snippet: get embedded resource

by softlion 16. November 2009 18:07

 

       public static string GetEmbeddedTextResource(string fullyQualifiedNameOfResource)
        {
            var assembly = Assembly.GetCallingAssembly();

            using (var reader = new StreamReader(assembly.GetManifestResourceStream(fullyQualifiedNameOfResource)))
            {
                //Automatic close/dispose
                return reader.ReadToEnd();
            }
        }

        public static byte[] GetEmbeddedBinaryResource(string fullyQualifiedNameOfResource)
        {
            var assembly = Assembly.GetCallingAssembly();
            byte[] result;

            using (var stream = assembly.GetManifestResourceStream(fullyQualifiedNameOfResource))
            {
                var reader = new BinaryReader(stream);
                result = reader.ReadBytes((int)stream.Length);
                reader.Close();
            }

            return result;
        }

 

Tags:

Silverlight 3 – Tip of the month #2 - Using Data Binding with Static Resources in Silverlight

by softlion 10. October 2009 13:06

 

In his blog, Pedro wrote a post showing how to bind a property to a static resource where the resource name is given by the binded property. But his solution needs WPF libraries and does not work in Silverlight.

This is the first way to write it which comes to mind. But it doesn’t work as StaticResource does not support Binding (it is static by nature).

<Image Source="{StaticResource {Binding AvailabilityStateResourceName}}" />

 

The solution I came with is elegant. It uses a custom converter to find the resource in the application resource dictionary. The binding source must be a string containing the name of the static resource.

 

<Image Source="{Binding AvailabilityStateResourceName, Converter={StaticResource StaticResourceConverter}}" />

 

To use it, declare a static resource refering to the converter like this:

<Application ...
             xmlns:shc="clr-namespace:Softlion.Silverlight.Shared.Converters;assembly=Softlion.Silverlight.Shared" 
             >
    <Application.Resources>
        <shc:StaticResourceConverter x:Key="StaticResourceConverter" />
        ...
    </Application.Resources>
</Application>

 

And the source code of the StaticResourceConverter which is simple and straightforward:

using System;
using System.Windows.Data;
using System.Windows;

namespace Softlion.Silverlight.Shared.Converters
{
    /// <summary>
    /// Enables a binding on a static resource in the application resource dictionary.
    /// Convert from a static resource name to a resource instance in the application resource dictionary.
    /// </summary>
    public class StaticResourceConverter : IValueConverter
    {
        #region IValueConverter Members
        /// <summary>
        /// Convert from a static resource name to a resource instance in the application resource dictionary.
        /// </summary>
        /// <param name="value">must be a reference to an existing resource in the application resource dictionary</param>
        /// <param name="targetType">defined by the resource</param>
        /// <param name="parameter">not used</param>
        /// <param name="culture">not used</param>
        /// <returns>A resource in the Application.Current.Resources dictionary</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is string))
                throw new ArgumentException("value");

            return Application.Current.Resources[value];
        }

        /// <summary>
        /// Not implemented, no use
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}

SL3 – Sunday reviews

by softlion 4. October 2009 10:12

Bryant Likes wrote a good post about choosing how to add a functionality to an existing control : behaviour, subclassing or attached property along with an example for each.

Behaviors vs Subclassing in Silverlight

 

Timmy Kokke wrote a detailed post about how to style a treeview.

http://geekswithblogs.net/tkokke/archive/2009/06/29/styling-a-treeview-in-silverlight-and-blend-3.aspx

Silverlight 3 - Tip of the month #1 – Binding Color property

by softlion 4. October 2009 08:04

In XAML you can not bind a property of type Color directly. I’m not sure why. Maybe because it is a structure (ie: value type).

You can bind a Brush instead :

 

[DataMember]
internal Color color;
public Brush DisplayColorBrush { get { return new SolidColorBrush(color); } }

 

Binding to a static color resource is working though:

<TextBlock.Foreground>
    <SolidColorBrush Color="{StaticResource UserOffersInfoColor}"/>
</TextBlock.Foreground>

 

<Application.Resources>
    <Color x:Key="UserOffersInfoColor">#FFE97F26</Color>
</Application.Resources>

 

Binding a Text property in a Run tag of a TextBlock tag doesn’t work either. You must bind to the Text property of the TextBlock tag instead.

<TextBlock Margin="6,3" FontWeight="Bold" FontSize="12">
    <Run Text="Chips: "/>
    <Run Text="{Binding ChipsRemaining}"/>
</TextBlock>

What outbound ip is used by windows and when ?

by softlion 3. September 2009 08:54

Or how the hell windows selects a network interface and an ip address from that interface (if it has more than one) for outbound packets ?

This excellent and easy to understand post from the Microsoft Network Team explain it all, along with differences between Win3K/XP and Win2K8/Vista.

Read it there: http://blogs.technet.com/networking/archive/2009/04/24/source-ip-address-selection-on-a-multi-homed-windows-computer.aspx

 

I repro it here: More...