A day with .Net

My day to day experince in .net

Archive for September, 2013

Exception Logging in Web API using ELMAH.

Posted by vivekcek on September 28, 2013

In this post i will show you how to implement exception logging in Web Api.

1. Install ELMAH Mvc from Nuget.

2. Create an exception filter.

    public class WebApiExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(actionExecutedContext.Exception));
        }
    }

3. Now use this exception filter in your web api methods.

        [HttpGet]
        [WebApiExceptionFilter]
        public string GetName()
        {
            throw new Exception("Test");
           
        }
Advertisements

Posted in MVC | Leave a Comment »

Multiple Get methods in Web API controller -Ninject.

Posted by vivekcek on September 28, 2013

When you have multiple get methods in a web api controller like below.

    public class WebApiController : ApiController
    {
        private IMyInterface _iMyInterface = null;

        public WebApiController(IMyInterface iMyInterface)
        {
            _iMyInterface = iMyInterface;
        }

        [HttpGet]
        public string GetName()
        {
            return _iMyInterface.Name;
        }

        [HttpGet]
        public string GetTest()
        {
            return _iMyInterface.Name;
        }
    }

Then you try to call ‘http://localhost:58956/api/WebApi/GetName‘ you may get an error like.

“ExceptionMessage”:”Multiple actions were found that match the request:

8

This is a simple route issue.The default route setting in WebApiConfig is shown below.

9

Change the above to.

10

Posted in MVC | Tagged: , | Leave a Comment »

Formatting Web Api json result to display in chrome.

Posted by vivekcek on September 28, 2013

Add the below code in WebApiConfig under App_Start.

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);

7

Posted in MVC | Tagged: , | Leave a Comment »

Dependency injection WEB API and Ninject.

Posted by vivekcek on September 28, 2013

In this post i will explain how to inject dependency to Web Api controller using Ninject.

1. Create a basic MVC application.

2. In the Model folder add an interface and its implementation as given below.

 public interface IMyInterface
    {
        string Name { get; set; }
    }
public class MyClass:IMyInterface
    {
        public string Name
        {
            get
            {
                return "vivek";
            }
            set
            {
                this.Name = value;
            }
        }
    }

3. Through Nuget add Ninject for MVC 3 as shown below. This package is designed for MVC. This package will also install Ninject core and Ninject webCommon.

1

4. After installation go to App_Start folder. You can see a new class named ‘NinjectWebCommon.cs’ is created. Open this class.

2

5. Inside the class Under the RegisterServices() method add the mapping of your interface to implementation.

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind<IMyInterface>().To<MyClass>();
}    

6. Go to Nuget again and add ‘Ninject.WebApi.DependencyResolver’.

3

7. In the NinjectWebCommon.cs file undel CreateKernal() method add the below code.

System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);

4

8. Now add Web Api controller

 public class WebApiController : ApiController
    {
        private IMyInterface _iMyInterface = null;

        public WebApiController(IMyInterface iMyInterface)
        {
            _iMyInterface = iMyInterface;
        }

        [HttpGet]
        public string GetName()
        {
            return _iMyInterface.Name;
        }
    }

9. Now call the Web Api in google chrome ‘http://localhost:58956/api/WebApi/GetName‘.You can saw the below result.

5

Posted in MVC | Tagged: , , | 1 Comment »