How to return a PDF from a Web API application

Some Server side code to return PDF (Web Api). [HttpGet] [Route(“documents/{docid}”)] public HttpResponseMessage Display(string docid) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest); var documents = reader.GetDocument(docid); if (documents != null && documents.Length == 1) { var document = documents[0]; docid = document.docid; byte[] buffer = new byte[0]; //generate pdf document MemoryStream memoryStream = new MemoryStream(); MyPDFGenerator.New().PrintToStream(document, memoryStream); … Read more

Web API optional parameters

I figured it out. I was using a bad example I found in the past of how to map query string to the method parameters. In case anyone else needs it, in order to have optional parameters in a query string such as: ~/api/products/filter?apc=AA&xpc=BB ~/api/products/filter?sku=7199123 you would use: [Route(“products/filter/{apc?}/{xpc?}/{sku?}”)] public IHttpActionResult Get(string apc = null, … Read more

How to prevent a single object property from being converted to a DateTime when it is a string

What one can do is to add a custom JsonConverter to the InputModel type to temporarily toggle JsonReader.DateParseHandling to None: [JsonConverter(typeof(DateParseHandlingConverter), DateParseHandling.None)] class InputModel { public string Name { get; set; } public object Value { get; set; } } public class DateParseHandlingConverter : JsonConverter { readonly DateParseHandling dateParseHandling; public DateParseHandlingConverter(DateParseHandling dateParseHandling) { this.dateParseHandling = … Read more

Asp.Net WebApi2 Enable CORS not working with AspNet.WebApi.Cors 5.2.3

I’ve created a pared-down demo project for you. Source: https://github.com/bigfont/webapi-cors Api Link: https://cors-webapi.azurewebsites.net/api/values You can try the above API Link from your local Fiddler to see the headers. Here is an explanation. Global.ascx All this does is call the WebApiConfig. It’s nothing but code organization. public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { … Read more

How to implement oauth2 server in ASP.NET MVC 5 and WEB API 2 [closed]

There is a brilliant blog post from Taiseer Joudeh with a detailed step-by-step description. Part 1: Token Based Authentication using ASP.NET Web API 2, Owin, and Identity Part 2: AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity Part 3: Enable OAuth Refresh Tokens in AngularJS App using ASP .NET Web API 2, … 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

CORS: credentials mode is ‘include’

The issue stems from your Angular code: When withCredentials is set to true, it is trying to send credentials or cookies along with the request. As that means another origin is potentially trying to do authenticated requests, the wildcard (“*”) is not permitted as the “Access-Control-Allow-Origin” header. You would have to explicitly respond with the … Read more

JsonConverter with Interface

How to automatically select a concrete type when deserializing an interface using Json.NET The easiest way to solve your problem is to serialize and deserialize your JSON (on both the client and server sides) with TypeNameHandling = TypeNameHandling.Auto. If you do, your JSON will include the actual type serialized for an IFIeld property, like so: … Read more