Converting json results to a date

 

Solution1:

var date = new Date(parseInt(jsonDate.substr(6)));

 

Solution2:

var x = [{
    "id": 1,
    "start": "\/Date(1238540400000)\/"
}, {
    "id": 2,
    "start": "\/Date(1238626800000)\/"
}];

var myDate = new Date(x[0].start.match(/\d+/)[0] * 1);


 

Source: https://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date

https://stackoverflow.com/questions/1244094/converting-json-results-to-a-date

 

 

Advertisements

How to check if IEnumerable is null or empty?

public static class Utils {
    public static bool IsAny<T>(this IEnumerable<T> data) {
        return data != null && data.Any();
    }
}

Source: https://stackoverflow.com/questions/5047349/how-to-check-if-ienumerable-is-null-or-empty

 

 

 

Request Entity Too Large Exception in Web API

The UploadReadAheadSize property establishes the number of bytes a Web server will read into a buffer and pass to an ISAPI extension. This occurs once per client request. The ISAPI extension receives any additional data directly from the client. The range is from 0 to &HFFFFFFFF (4 GB).

For more details on this property please check the URL – https://msdn.microsoft.com/en-us/library/ms525310%28v=vs.90%29.aspx

 

To navigate to this setting, use the following steps:

  • Launch “Internet Information Services (IIS) Manager”
  • Expand the Server field
  • Expand Sites
  • Select the site your application is in.
  • In the Features section, double click “Configuration Editor”
  • Under “Section” select: system.webServer>serverRuntime

 

For my case, customer had maximum file size less than 50 MB, so we set it to 52428800 i.e. 50 MB.

Please note that the value is in Bytes.

 

This resolved the issue for us and file uploads and downloads worked fine.

 

I hope this helps!

 

SOURCE: https://blogs.msdn.microsoft.com/dsnotes/2015/08/21/large-file-upload-failure-for-web-application-calling-wcf-service-413-request-entity-too-large/

 

 

 

Setting IIS and ASP.NET POST Size Request Limits

IIS Setting: Request Limits

The first setting is the IIS Request Limit settings which is installed as part of the Request Filtering IIS Module. IIS Request Filtering is an optional IIS feature, but it is a required install feature when you install ASP.NET in IIS so you can assume it will be installed if ASP.NET is enabled in IIS.

The following sets the max POST buffer size to 500 megs. The value is specified in bytes.

<configuration>
    <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="500000000" />
          </requestFiltering>
        </security>
    <system.webServer>
</configuration>    

ASP.NET Setting: httpRuntime maxRequestLength

ASP.NET uses the httpRuntime element to control a number of runtime related features including the allowed inbound request length. The inbound data size is checked very early in the request cycle and makes a request fail immediately.

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="500000000"  executionTimeout="120" />
    </system.web>
<configuration>

Source: https://webconnection.west-wind.com/docs/_4lp0zgm9d.htm

 

Web API and C# out Values

 

public IHttpActionResult GetResponse(int id)
{
    int outputInt;
    string outputString;

    YourMethodWithOutParameters(id, out outputInt, out outputString);

    return Ok(new
    {
        Id = id,
        OutputInt = outputInt,
        OutputString = outputString,
    });
}

 

Source: https://stackoverflow.com/questions/39944732/web-api-and-c-sharp-out-values

 

Check for Balanced Parenthesis in a String

 

public static bool IsBalanced(string input)
    {
        Dictionary<char, char> bracketPairs = new Dictionary<char, char>() {
            { '(', ')' },
            { '{', '}' },
            { '[', ']' },
            { '<', '>' }
        };

        Stack<char> brackets = new Stack<char>();

        try
        {
            // Iterate through each character in the input string
            foreach (char c in input)
            {
                // check if the character is one of the 'opening' brackets
                if (bracketPairs.Keys.Contains(c))
                {
                    // if yes, push to stack
                    brackets.Push(c);
                }
                else
                // check if the character is one of the 'closing' brackets
                    if (bracketPairs.Values.Contains(c))
                {
                    // check if the closing bracket matches the 'latest' 'opening' bracket
                    if (c == bracketPairs[brackets.First()])
                    {
                        brackets.Pop();
                    }
                    else
                        // if not, its an unbalanced string
                        return false;
                }
                else
                    // continue looking
                    continue;
            }
        }
        catch
        {
            // an exception will be caught in case a closing bracket is found, 
            // before any opening bracket.
            // that implies, the string is not balanced. Return false
            return false;
        }

        // Ensure all brackets are closed
        return brackets.Count() == 0 ? true : false;
    }

Source: https://www.codeproject.com/Tips/1175562/Check-for-Balanced-Parenthesis-in-a-String