Sammy Ageil

Lead by example

IsString C# and VB.NET Extension method

May 28
by Sammy Ageil 28. May 2011 21:19

Here is a quick and dirty regex based IsString() .Net extension method.

C# version

 

public static bool IsString(this object word)
        {
            return word == null ? false : Regex.IsMatch(word.ToString(), @"^[A-Za-z]+\Z");
        }

 

VB.Net version

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsString(word As Object) As Boolean
	Return If(word Is Nothing, False, Regex.IsMatch(word.ToString(), "^[A-Za-z]+\Z"))
End Function
 

 

The method will match strings only no spaces or other characters will be matched

Enjoy

Tags: , ,

IsNumeric .Net extension in C # and VB.NET

May 27
by Sammy Ageil 27. May 2011 11:10

Here is a quick and dirty IsNumeric extension method in C#. the method uses Regex to determine the result

C# version

public static bool IsNumeric(this object number)
        {
            return number == null ? false : Regex.IsMatch(number.ToString(), @"^[-+]?[0-9]+(\.[0-9]{1,2})?\Z");
           
        }

 

VB.NET version

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsNumeric(number As Object) As Boolean
	Return If(number Is Nothing, False, Regex.IsMatch(number.ToString(), "^[-+]?[0-9]+(\.[0-9]{1,2})?\Z"))

End Function
 

 

The pattern will match -##.##, +##.## ,##.## and #.#

 

Enjoy :-)

Tags: , ,

Post strongly typed model with jQuery ajax and ASP.NET MVC 3.0 and json

May 15
by Sammy Ageil 15. May 2011 15:47

Today I needed to post a strongly typed model using jquery and MVC 3.0 using razor engine.

My first thought was this should be easy, just hijack the submit button and send the request. well to my surprise, I was wrong :-)

The post was processed OK but my model didn't have ant of its properties populated.all of model's properties were null

The solution was to hijack the form's submit event and post the serialized form using jQuery's ajax function.

Note, the $(this).serialize() call in the body of the ajax function.

HTML and javascript

 

<form id="frmCreateUser" action="" method="post" >
    
    <div class="bordered" id="UserForm">
        <fieldset>
            <legend>User Information</legend>

           
                @Html.LabelFor(m => m.UserName, "UserName:")
                @Html.TextBoxFor(m => m.UserName, new { @class = "text-box" })
                @Html.ValidationMessageFor(m => m.UserName) <br /> 

                @Html.LabelFor(m => m.Password, "Password:")
                @Html.PasswordFor(m => m.Password, new { @class = "text-box" })
                @Html.ValidationMessageFor(m => m.Password) <br />

                @Html.LabelFor(x=>x.Email,"Email:")
                @Html.TextBoxFor(x=>x.Email)
                @Html.ValidationMessageFor(x=>x.Email)<br />


                <input id="createButton" type="submit" value="Create" />
           
        </fieldset>
    </div>

 

<script type="text/javascript">
        $(document).ready(function () {
         
            $("#frmCreateUser").submit(function (event) {
               
                $.ajax(
                {
                    url: '@Url.Action("CreateUser", "dashboard",new {area="Security"})',

                    dataType: 'json',
                    data:$(this).serialize(),
                    type:'POST',
                    success: function (result) {
                        
                        alert(result);
                    },
                    error: function (xhr) {
                        alert(xhr.statusText);
                    }

                });
                event.preventDefault();
            });
        });
    </script>

 

here is my action.

[HttpPost]
        public JsonResult CreateUser(User model)
        {
            bool isSuccess=false;
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
            return Json(createStatus.ToString(), JsonRequestBehavior.AllowGet);
        }

Tags: , ,

Request is not available in this context exception in Global.asax's Application_Start IIS 7 Integrated mode

May 08
by Sammy Ageil 8. May 2011 23:11

Recently we started upgrading an old Asp.NET 2.0 application to Framework 4 and IIS 7. Upgrades went OK and the application started working on test environment hosted on an older Windows 2003 server. Once the application went to production we started seeing "Request is not available in this context".
We knew the application calls a licensing service in the Application_Start() event.

obviously this exception related to IIS7 because the application worked without any issues on the test environment. After some research we found out this issue is related to IIS 7 Integrated mode. The solution was easy. I created a static constructor in global.asax and used a static readonly variable to use in the Application_Start() event.

Here is the code

static readonly HttpRequest initialRequest;
        static MyApplication()
        {

            initialRequest = HttpContext.Current.Request;
           
            
        }

    protected void Application_Start()
        {
      //Get the IP Adddress of the host to send for the licensing service
      var domainUrl= initialRequest.ServerVariables["LOCAL_ADDR"];
            
        }

I hope this can help some

Tags: ,

Poorly named domains - a must see

May 07
by Sammy Ageil 7. May 2011 07:48

This page is a must see

http://www.blabla.co.za/2011/04/01/top-list-7-poorly-named-websites/

That's why we should think first then act?

Tags: