ASP.NET WebApi Error

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

 

When working with ASP.NET WebApi you might get following error:

122
{"ExceptionType":"System.NullReferenceException","Message":"Object reference not set to an instance of an object.","StackTrace":"\u000d\u000aServer stack trace: \u000d\u000a   at System.Web.Http.ValueProviders.Providers.RouteDataValueProvider.GetRoutes(IHttpRouteData routeData)\u000d\u000a
d2
   at System.Web.Http.ValueProviders.Providers.RouteDataValueProvider.<>c__DisplayClass2.<.ctor>b__0()\u000d\u000a   at System.Lazy`1.CreateValue()\u000d\u000a\u000d\u000aException rethrown at [0]: \u000d\u000a
143
   at System.Web.Http.ValueProviders.Providers.RouteDataValueProvider.GetRoutes(IHttpRouteData routeData)\u000d\u000a   at System.Web.Http.ValueProviders.Providers.RouteDataValueProvider.<>c__DisplayClass2.<.ctor>b__0()\u000d\u000a   at System.Lazy`1.CreateValue()\u000d\u000a   at System.Lazy`1.LazyInitValue()\u000d\u000a
14f
   at System.Web.Http.ValueProviders.Providers.NameValueCollectionValueProvider.GetValue(String key)\u000d\u000a   at System.Web.Http.ValueProviders.Providers.CompositeValueProvider.<>c__DisplayClass9.<GetValue>b__4(IValueProvider provider)\u000d\u000a   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\u000d\u000a
10f
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\u000d\u000a   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)\u000d\u000a   at System.Web.Http.ValueProviders.Providers.CompositeValueProvider.GetValue(String key)\u000d\u000a
90
   at System.Web.Http.ModelBinding.Binders.TypeMatchModelBinder.GetCompatibleValueProviderResult(ModelBindingContext bindingContext)\u000d\u000a
a2
   at System.Web.Http.ModelBinding.Binders.TypeMatchModelBinderProvider.GetBinder(HttpActionContext actionContext, ModelBindingContext bindingContext)\u000d\u000a
12c
   at System.Web.Http.ModelBinding.Binders.CompositeModelBinder.<>c__DisplayClass6.<GetBinder>b__1(ModelBinderProvider provider)\u000d\u000a   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()\u000d\u000a   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\u000d\u000a
f0
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)\u000d\u000a   at System.Web.Http.ModelBinding.Binders.CompositeModelBinder.GetBinder(HttpActionContext actionContext, ModelBindingContext bindingContext)\u000d\u000a
9a
   at System.Web.Http.ModelBinding.Binders.CompositeModelBinder.BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)\u000d\u000a
a2
   at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindParameterValue(HttpActionContext actionContext, HttpParameterBinding parameterBinding)\u000d\u000a
103
   at System.Web.Http.ModelBinding.DefaultActionValueBinder.<>c__DisplayClass1.<BindValuesAsync>b__0(RequestContentReadKind contentReadKind)\u000d\u000a   at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass38.<ToAsyncVoidTask>b__37()\u000d\u000a
76
   at System.Threading.Tasks.TaskHelpe
rs.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}
0

This error is caused by routing initialization like following one:

  routes.MapHttpRoute(

                name: "GetAccountData2",

                routeTemplate: "api/{controller}",

                defaults: new AccountData()

            );

 

To fix it change routing initialization like shown below:

   routes.MapHttpRoute(

                name: "GetAccountData2",

                routeTemplate: "api/{controller}",

                defaults: new AccountData { Roles = new List<string>() , UserName = "" }

            );

 

Notice the difference “{ Roles = new List<string>() , UserName = "" } “


Posted Feb 26 2012, 02:31 AM by Damir Dobric
Filed under: , ,

Comments

Jarek wrote re: ASP.NET WebApi Error
on 06-05-2012 5:45

I don't even know what to say, this made thngis so much easier!

developers.de is a .Net Community Blog powered by daenet GmbH.