Add a custom response header in ApiController

You can explicitly add custom headers in a method like so: [HttpGet] [Route(“home/students”)] public HttpResponseMessage GetStudents() { // Get students from Database // Create the response var response = Request.CreateResponse(HttpStatusCode.OK, students); // Set headers for paging response.Headers.Add(“X-Students-Total-Count”, students.Count()); return response; } For more information read this article: http://www.jerriepelser.com/blog/paging-in-aspnet-webapi-http-headers/

The route template separator character ‘/’ cannot appear consecutively – Attribute routing issue

The reason for the above error is that I am using an additional “https://stackoverflow.com/” in the route attribute for the action. The action defined in the above controller should be as follows: [HttpGet] [Route(“marketdata/tickerinfo”)] public IHttpActionResult TickerInfo(string currencyPair) {

Creating new IHttpActionResult action result methods

There’s no convenience method for no-content result because, by default, when a action returns void, the response will have the HTTP status 204. If you wish to explicitly indicate that on the action, you could also return a StatusCode(HttpStatusCode.NoContent) from your action or a ResponseMessage(new HttpResponseMessage(HttpStatusCode.NoContent)). The Unauthorized() convenience method gives you a 401 status … Read more

How to return JSON in an ApiController for a single method?

If you can’t make a global change to force responses as JSON, then try: [Route(“api/Player/videos”)] public HttpResponseMessage GetVideoMappings() { var model = new MyCarModel(); return Request.CreateResponse(HttpStatusCode.OK,model,Configuration.Formatters.JsonFormatter); } OR [Route(“api/Player/videos”)] public IHttpActionResult GetVideoMappings() { var model = new MyCarModel(); return Json(model); } If you want to change globally, then first go to YourProject/App_Start/WebApiConfig.cs and add: config.Formatters.XmlFormatter.SupportedMediaTypes.Remove( … Read more

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

ASP.NET Web API generate URL using Url.Action

Maybe the closest helper to Url.Action in Web API Controller is the Url.Link method which will generate the URL by Route name, Controller Name, Action Name and the route parameters (if needed). Here is a simple example The default App_start/RouteConfig.cs routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Home”, action = “Index”, id … Read more

Autofac RegisterInstance vs SingleInstance

Is the manual newing-up of the ServiceProductDataProvider with RegisterInstance not the same as the Register .SingleInstance() ?? The RegisterInstance allows you to register a single instance in AutoFac. The difference between RegisterInstance and RegisterType + SingleInstance methods is that the RegisterInstance method allows you to register an instance not built by Autofac. But both solution … Read more

Post byte array to Web API server using HttpClient

WebAPI v2.1 and beyond supports BSON (Binary JSON) out of the box, and even has a MediaTypeFormatter included for it. This means you can post your entire message in binary format. If you want to use it, you’ll need to set it in WebApiConfig: public static class WebApiConfig { public static void Register(HttpConfiguration config) { … Read more

How to configure Web Api 2 to look for Controllers in a separate project? (just like I used to do in Web Api)

I have just confirmed that this works fine. Things to check: References: Does your main Web API project reference the external class library? Routing: Have you set up any routes that might interfere with the external controllers? Protection Level: Are the controllers in the external library public? Inheritance: Do the controllers in the external library … Read more

UseOAuthBearerTokens vs UseOAuthBearerAuthentication

The UseOAuthBearerTokens extension method creates both the token server and the middleware to validate tokens for requests in the same application. Pseudocode from source using reflector: UseOAuthAuthorizationServer(); // authorization server middleware UseOAuthBearerAuthentication(ApplicationOAuthBearerProvider); // application bearer token middleware UseOAuthBearerAuthentication(ExternalOAuthBearerProvider); // external bearer token middleware

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)