WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Friday, September 28, 2012

Handling Unhandled Exception Asp.NET MVC

Well, having unhandled exception is bad in the first place but displaying user a default IIS broken exception page or the yellow broken code page is even worse. Not a good user experience. But as all code/software there is always bugs and all ways a edge case that breaks unexpectedly. These unhandled expections can be handled in controller level in Asp.NET MVC. Following is a code snipped of handling errors and routing the user to a more user friendly error page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class HandleErrorAttribute: System.Web.Mvc.HandleErrorAttribute
{
 public override void OnException(ExceptionContext filterContext)
 {
  string askerUrl = filterContext.RequestContext.HttpContext.Request.RawUrl;
 
  Exception exToLog = filterContext.Exception;
  exToLog.Source = string.Format("Source: {0}{1}Requested Path: {2}{1}",
    exToLog.Source, Environment.NewLine, askerUrl);
 
  //log your message somewhere
  Handle(exToLog);
 
  filterContext.ExceptionHandled = true;
  filterContext.Result = new System.Web.Mvc.RedirectResult("~/Error");
 
  base.OnException(filterContext);
         }
}
 
namespace YourNameSpace
{
    [HandleError]
    public class YourController : Controller
    {
        //controller code - Actions
    }
}


In the above code, once the error is handled it's redirected to an Error Page. A user friendly error page such as this from clipthetrip.com

Labels: , ,

Saturday, September 1, 2012

Beer in my belly - IX


Acbc dizzy

Harviestoun bitter


Marooned on hog



liefmans goudenband

Labels:

Amazon SES (Simple Email Service) - Code

SES (simple email service) provided by Amazon is sleek easy to use email service. Unfortunately as always, anything new has limited documentation or overly complicated documentation. Following is a ready to use simple code snippet for using Amazon SES written in CSharp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
 
namespace Manager
{
    public class EmailManager
    {
        public string SendMail(List<string> toAddresses, List<string> ccAddress, List<string> bccAddress,
            string fromName, string fromAddress,
            string subject, string content,            
            string awsAccessKey, string awsSecretKey)
        {
            try
            {
                var amazonConfiguration = new AmazonSimpleEmailServiceConfig();
                var client = new AmazonSimpleEmailServiceClient(awsAccessKey, awsSecretKey, amazonConfiguration);
                 
                var destination = new Destination();
                destination.ToAddresses = toAddresses;
                destination.CcAddresses = ccAddress;
                destination.BccAddresses = bccAddress;
 
                var body = new Body()
                {
                    Html = new Content(content)
                };
 
                var emailSubject = new Content(subject);
                var message = new Message(emailSubject, body);
                var sendEmailRequest = new SendEmailRequest(string.Format("{0} <{1}>", fromName, fromAddress), destination, message);
 
                var response = client.SendEmail(sendEmailRequest);
                return response.SendEmailResult.MessageId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Note: Amazon SDK needs to be installed or AWSSDK.dll need to be referenced. Download AWS SDK here So where can I find the “awsAccessKey” (Access key id) and “awsSecretKey” (Secret Key). Well, first login into AWS portal and then head over to Security Credentials page.



In the Security Credentials page look for Access Credentials section and you should have your Access Key Id and your Secret key.

Labels: , ,