Tuesday, 19 November 2013

How to loop through characters in a string?

This can be done using a foreach loop.

Following example finds the count of letters in a given string.

int count = 0;

string str = "Welcome to C#!";

foreach(char c in str) {
  if(char.IsLetter(c)) {
    count++;
  }
}

Cosole.WriteLine(count.ToString());
There are additional static methods on char that will tell you if the character in question is punctuation, a digit, etc.

How to convert a char to lower case in C#?

Char.ToLower Method

The method returns lowercase equivalent of given character, or the unchanged value, if the char is already lowercase or not alphabetic.

Example
Char.ToLower('A')

Tuesday, 5 November 2013

Create your own helper functions in ASP.NET MVC

Razor’s @helper syntax has made it pretty easy to create your own helper functions for use in your views. For example open your ".cshtml" file and add the following code directly after the @model line.

@helper Truncate(string
input, int length)
 {
    if (input.Length <= length) {
        @input
    } else {
        @input.Substring(0, length)...
    }
} 

This helper method takes a string and a maximum length to allow. If the text supplied is shorter than the length specified, the helper outputs it as-is. If it is longer, then it truncates the text and renders “…” for the remainder.

Now you can refer the Truncate method in your view. So if your Model has a Title property

@Truncate(Model.Title, 25)

Read more>>

Make the helper method accessible to all views by defining it in a CSHTML file under App_Code folder. Click here to read more >>

Wednesday, 23 October 2013

Catching WCF FaultException in ASP.NET Web Application

If you are making WCF method calls from your app it will be handy if your code give error details when something goes wrong with the WCF call. This can be done by adding a catch(System.ServiceModel.FaultException){} block within your code.
ServiceClient serviceClient;

try
{

//create ServiceClient 

//Service method call

}
catch (System.ServiceModel.FaultException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
finally
{
if (serviceClient != null)
serviceClient.close()
}
With this code you can put a break point within the catch(System.ServiceModel.FaultException ex) block and inspect the fault exception as seen in the image below.

Disabling custom errors using TrySkipIisCustomErrors

HttpResponse.TrySkipIisCustomErrors Property can be used to enable / disable custom errors on IIS 7.0

The TrySkipIisCustomErrors property is used only when your application is hosted in IIS 7.0. When running in Classic mode in IIS 7.0 the TrySkipIisCustomErrors property default value is true. When running in Integrated mode, the TrySkipIisCustomErrors property default value is false.

HTTP Errors

The <httpErrors> element allows you to configure custom error messages for your Web site or application. Custom error messages let you provide a friendly or a more informative response by serving a file, returning another resource, or redirecting to a URL when visitors to your site cannot access the content they requested. For example, you might want to customize each of the error message pages for your Web site to have the same look and feel as the rest of your site.

The <httpErrors> element contains a collection of elements, each of which defines an error message that IIS uses to respond to specific HTTP errors. You can add custom error messages to IIS by adding an <error> element to the <httpErrors> element in the Web.config file for your site, application, or URL. Each element uses the responseMode attribute to specify whether IIS serves static content, dynamic content, or redirects to a separate URL in response to an error.

Example HTTP Errors configuration

<system.webServer>
<httpErrors>
<error statusCode="404" prefixLanguageFilePath="" 
path="/404Error" responseMode="ExecuteURL" />
<error statusCode="410" path="/410Error" 
responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>

Read more >>

Tuesday, 22 October 2013

jQUery - read textbox value using its name

This can be done using jQuery's Attribute Equals Selector [name="value"] syntax. For example if name of the textbox is "email" then below script can be used to read the value.

$('input[name="email"]').val()