Browsed by
Category: Web

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

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

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