Application_Error in global.asax not catching errors in WebAPI

Abstract out your error handling logic from Application_Error into its own function. Create a Web API exception filter. //register your filter with Web API pipeline //this belongs in the Application_Start event in Global Application Handler class (global.asax) //or some other location that runs on startup GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute()); //Create filter public class LogExceptionFilterAttribute : ExceptionFilterAttribute { … Read more

MVC Attribute Routing Not Working

Not only do you have to have the call to routes.MapMvcAttributeRoutes() in your App_Start\RouteConfig.cs file, it must appear before defining your default route! I add it before that and after ignoring {resource}.axd{*pathInfo}. Just found that out trying to get attribute routing to work correctly with my MVC 5 website. public static void RegisterRoutes(RouteCollection routes) { … Read more

ASP.NET (OWIN) Identity: How to get UserID from a Web API controller?

You should be able to get user id on both MVC controller and web api controller by same extension method in identity 1.0 RTW package. Here is the extensions from identity package: namespace Microsoft.AspNet.Identity { public static class IdentityExtensions { public static string FindFirstValue(this ClaimsIdentity identity, string claimType); public static string GetUserId(this IIdentity identity); public … Read more

Order of execution with multiple filters in web api

Some things to note here: Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters. Authorization Filters -> Action Filters -> Exception Filters Now the problem that you seem to mention is related to having multiple filters of the same kind (ex: Multiple ActionFilterAttribute decorated on … Read more

AllowAnonymous not working with Custom AuthorizationAttribute

In the AuthorizeAttribute there is the following code: private static bool SkipAuthorization(HttpActionContext actionContext) { Contract.Assert(actionContext != null); return actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any(); } Include this method in your AuthorizeAttribute class then add the following to the top of your OnAuthorization method to skip authorization if any AllowAnonymous attributes are found: if (SkipAuthorization(actionContext)) return;

How to use System.Net.HttpClient to post a complex type?

The generic HttpRequestMessage<T> has been removed. This : new HttpRequestMessage<Widget>(widget) will no longer work. Instead, from this post, the ASP.NET team has included some new calls to support this functionality: HttpClient.PostAsJsonAsync<T>(T value) sends “application/json” HttpClient.PostAsXmlAsync<T>(T value) sends “application/xml” So, the new code (from dunston) becomes: Widget widget = new Widget() widget.Name = “test” widget.Price = … Read more