Returning binary file from controller in ASP.NET Web API

Returning binary file from controller in ASP.NET Web API

Source Try using a simple HttpResponseMessage with its Content property set to a StreamContent: // using System.IO; // using System.Net.Http; // using System.Net.Http.Headers; public HttpResponseMessage Post(string version, string environment, string filetype) { var path = @”C:\Temp\test.exe”; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(path, FileMode.Open, FileAccess.Read); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/octet-stream”); return result; } A few things to note about the stream used: You must not call stream.Dispose(), since Web API still needs to be able to access it when it processes the…

Read More Read More

How to make IIS7 play nice with Office Interop

How to make IIS7 play nice with Office Interop

Source I’ve just started working on a new project for which we had to dig out one of our older libraries. We wrote it to retrieve data from MS Excel workbooks using the Office interop assemblies, and thus far it has never given us issues. A slight change this time round was that we were using our library in an ASP.NET site rather than a desktop application. As a result I was faced with two errors that we’d never seen…

Read More Read More

Setting up MVC 5.2.3.0 site with .Net 4.6 on IIS 7.5 on 2008R2SP1

Setting up MVC 5.2.3.0 site with .Net 4.6 on IIS 7.5 on 2008R2SP1

Thank you for that. After a bit of Googling I documented my setup roughly like this: – Install .Net 3.5.1 Features via Server Manager -> Add features wizard – Install .Net 4.6: – Install IIS 7.5: — Make sure you include the following features in IIS: IIS management console, IIS Management Script and Tools, Management Service, Static content, Directory Browsing, HTTP Errors, HTTP Redirection, ASP, ASP .Net, ISAPI Extensions, ISAPI Filters, .Net Extensibility, Default Document, HTTP Logging, Logging Tools, Basic…

Read More Read More

ASP.NET MVC – Set custom IIdentity or IPrincipal

ASP.NET MVC – Set custom IIdentity or IPrincipal

Source I decided to use IPrincipal instead of IIdentity because it means I don’t have to implement both IIdentity and IPrincipal. Create the interface interface ICustomPrincipal : IPrincipal { int Id { get; set; } string FirstName { get; set; } string LastName { get; set; } } CustomPrincipal public class CustomPrincipal : ICustomPrincipal { public IIdentity Identity { get; private set; } public bool IsInRole(string role) { return false; } public CustomPrincipal(string email) { this.Identity = new GenericIdentity(email); }…

Read More Read More

WEB API 2 EXPLORING PARAMETER BINDING

WEB API 2 EXPLORING PARAMETER BINDING

Source This article demonstrates how to call or map Web API methods with different types of parameters using XML, Json and Urlencoded formats. It shows how simple parameters, objects parameters and array parameters can be sent either in the body of the Http request or in the Url itself. This all works per default in Web API and if that’s not enough, you can customize it yourself. code: https://github.com/damienbod/WebApiParameters Simple Parameters Example 1: Sending a simple parameter in the Url 1 2…

Read More Read More

Send string array to web api

Send string array to web api

You can send it in the body or the URL URL: // http://localhost:49407/api/values/example6?paramsObject=2,paramsObject=4,paramsObject=9 [Route(“example6”)] [HttpGet] public string GetListFromUri([FromUri] List<int> paramsObject) { if (paramsObject != null) { return “recieved a list with length:” + paramsObject.Count; } return “NOTHING RECIEVED…”; } or in the body: [Route(“example8”)] [HttpPost] public string GetListFromBody([FromBody] List<ParamsObject> paramsList) { if (paramsList != null) { return “recieved a list with length:” + paramsList.Count; } return “NOTHING RECIEVED…”; } User-Agent: Fiddler Content-Type: application/json Host: localhost:49407 Content-Length: 91 [{“Id1″:3,”Id2″:76,”Id3”:19},{“Id1″:56,”Id2″:87,”Id3”:94},{“Id1″:976,”Id2″:345,”Id3”:7554}] Here’s example…

Read More Read More

Don’t Block on Async Code

Don’t Block on Async Code

Source This is a problem that is brought up repeatedly on the forums and Stack Overflow. I think it’s the most-asked question by async newcomers once they’ve learned the basics. UI Example Consider the example below. A button click will initiate a REST call and display the results in a text box (this sample is for Windows Forms, but the same principles apply to any UI application). // My “library” method. public static async Task<JObject> GetJsonAsync(Uri uri) { using (var client =…

Read More Read More

Visual studio connect Oracle failed

Visual studio connect Oracle failed

Error Message: “failed to connect to server or failed to parse connect string” 1. Navigate to visual studio tools for oracle directory and under the network/admin folder look for the sqlnet.ora file 2. modify and change “SQLNET.AUTHENTICATION_SERVICES= (NTS)” to “SQLNET.AUTHENTICATION_SERVICES= (NONE)” 3. Restart Visual Studio and Try again.

Electronic Fuel Injection (PGM-FI) of the Honda CBR125R

Electronic Fuel Injection (PGM-FI) of the Honda CBR125R

Source After installing the Athena 166cc cylinder and piston bore up kit, the Kijima and Daytona air filters, and the Akrapovic carbon racing exhaust, the quantity of air inhaled by the engine has been increased. As explained in other posts, in a gasoline internal combustion engine, it is necessary to inject the correct amount of fuel (gasoline) depending on the quantity of air which the engine is breathing. This mass stoichiometric ratio is fixed (air-fuel mixture ratio), and, theoretically, should be equal…

Read More Read More

Use the On-Premises Organizational Authentication Option (ADFS) With ASP.NET in Visual Studio 2013

Use the On-Premises Organizational Authentication Option (ADFS) With ASP.NET in Visual Studio 2013

Source This afternoon my good friend Pranav Rastogi pointed out that we don’t have a walkthrough showing how to use the On-Premises option for organizational authentication in the new ASP.NET project templates in VS2013 – AKA hooking up your web app to an ADFS instance. You know what? He was right! With all the excitement around the new capabilities for cloud-based project, we didn’t cover this specific feature as that is largely a refactoring of what was already available in the Identity and…

Read More Read More