Amazon Ad

Wednesday, 29 April 2015

Application_Error Not Invoked From Global.asax

Hi ,

If you have implemented Application_Error event in Global.asax and its not invoked, after all the hard work, You need to make sure to do the following

1. Sometimes in the web.config the CustomError property is set to Off. In this case set it to On.

2. Make sure your Global.asax's bin file (app_global.dll) exists and the modified date is same as your Global.asax file.

3. If you have implemented RadCompression, It will not invoke the Application_Error event until you add the following in your web.config file

<telerik.web.ui>  
    <radCompression>   
        <excludeHandlers>
            <add handlerPath="Global.asax" matchExact="true"/>
        </excludeHandlers>
    </radCompression>
</telerik.web.ui>

4. Still if you are not able to implement it, Use HttpModule to trap an error.

These are some of the common causes of not invoking the error event.

Thanks

Tuesday, 28 April 2015

405 Method not found in ASP.NET Web API and Angular.js

Hi,

I was facing a strange problem when implementing web api in asp.net with Angular.js. The web api method was absolutely fine and was working locally. But when it was published on the development server, It gave the error 405 method not found. I added the CORS in web.api, in web.config and in the controller by enabling cors (Adding the line [EnableCors("*", "*", "*")] before the controller class). But no use it was still giving me the same message with OPTIONS method not found.

Finally, I resolved it by adding the config section in my angular code where i was creating my module. Below is the code. After adding this it resolved all the issues i was facing.

Thursday, 16 April 2015

How make faster asp.net websites in ASP.NET 4.5 with EF 6

Hi Guys,

I was able to successfully create a responsive and very fast website with EF6 and ASP.NET 4.5. Here is how i was able to do it successfully.

1. Create a basepage which all the webforms inherits and add the following code in the constructor of the basepage

a. you need to install a nuget package

PM> Install-Package EFInteractiveViews

b. Then add below code in your constructor of basepage class
public BasePage()
        {
            try
            {
                using (var ctx = new LPUOnlineEntities())
                {
                    InteractiveViews.SetViewCacheFactory(ctx, new SqlServerViewCacheFactory

(ctx.Database.Connection.ConnectionString));
                }
            }
            catch (Exception ex)
            {

            }
        }


2. Enable ViewState Compression

a. Create two methods in your basepage class
protected override object LoadPageStateFromPersistenceMedium()
        {
            string viewState = Request.Form["__VSTATE"];
            byte[] bytes = Convert.FromBase64String(viewState);
            bytes = Compressor.Decompress(bytes);
            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(Convert.ToBase64String(bytes));
        }

        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            LosFormatter formatter = new LosFormatter();
            StringWriter writer = new StringWriter();
            formatter.Serialize(writer, viewState);
            string viewStateString = writer.ToString();
            byte[] bytes = Convert.FromBase64String(viewStateString);
            bytes = Compressor.Compress(bytes);
            //ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
            ScriptManager.RegisterHiddenField(this, "__VSTATE", Convert.ToBase64String(bytes));
        }

b. A class which compresses the data

using System.IO;
using System.IO.Compression;

public static class Compressor
{

    public static byte[] Compress(byte[] data)
    {
        MemoryStream output = new MemoryStream();
        GZipStream gzip = new GZipStream(output,
                          CompressionMode.Compress, true);
        gzip.Write(data, 0, data.Length);
        gzip.Close();
        return output.ToArray();
    }

    public static byte[] Decompress(byte[] data)
    {
        MemoryStream input = new MemoryStream();
        input.Write(data, 0, data.Length);
        input.Position = 0;
        GZipStream gzip = new GZipStream(input,
                          CompressionMode.Decompress, true);
        MemoryStream output = new MemoryStream();
        byte[] buff = new byte[64];
        int read = -1;
        read = gzip.Read(buff, 0, buff.Length);
        while (read > 0)
        {
            output.Write(buff, 0, read);
            read = gzip.Read(buff, 0, buff.Length);
        }
        gzip.Close();
        return output.ToArray();
    }
}

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...