How to return a file (FileContentResult) in ASP.NET WebAPI

In a regular MVC controller, we can output pdf with a FileContentResult. public FileContentResult Test(TestViewModel vm) { var stream = new MemoryStream(); //… add content to the stream. return File(stream.GetBuffer(), “application/pdf”, “test.pdf”); } But how can we change it into an ApiController? [HttpPost] public IHttpActionResult Test(TestViewModel vm) { //… return Ok(pdfOutput); } Here is what … Read more

Post parameter is always null

Since upgrading to RC for WebAPI I’m having some real odd issue when calling POST on my WebAPI. I’ve even gone back to the basic version generated on new project. So: public void Post(string value) { } and calling from Fiddler: Header: User-Agent: Fiddler Host: localhost:60725 Content-Type: application/json Content-Length: 29 Body: { “value”: “test” } … Read more

Optional query string parameters in ASP.NET Web API

I need to implement the following WebAPI method: /api/books?author=XXX&title=XXX&isbn=XXX&somethingelse=XXX&date=XXX All of the query string parameters can be null. That is, the caller can specify from 0 to all of the 5 parameters. In MVC4 beta I used to do the following: public class BooksController : ApiController { // GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01 public string GetFindBooks(string author, string … Read more

Returning http status code from Web Api controller

I’m trying to return a status code of 304 not modified for a GET method in a web api controller. The only way I succeeded was something like this: public class TryController : ApiController { public User GetUser(int userId, DateTime lastModifiedAtClient) { var user = new DataEntities().Users.First(p => p.Id == userId); if (user.LastModified <= lastModifiedAtClient) … Read more

Microsoft Web API: How do you do a Server.MapPath?

Since Microsoft Web API isn’t MVC, you cannot do something like this: var a = Request.MapPath(“~”); nor this var b = Server.MapPath(“~”); because these are under the System.Web namespace, not the System.Web.Http namespace. So how do you figure out the relative server path in Web API ? I used to do something like this in … Read more

Multiple actions were found that match the request in Web Api

I keep getting this error when I try to have 2 “Get” methods Multiple actions were found that match the request: webapi I been looking around at the other similar questions about this on stack but I don’t get it. I have 2 different names and using the “HttpGet” attribute [HttpGet] public HttpResponseMessage Summary(MyVm vm) … Read more

Exposing localhost to the internet via tunneling (using ngrok): HTTP error 400: bad request; invalid hostname

From previous versions of the question, there is this: Browse website with ip address rather than localhost, which outlines pretty much what I’ve done so far…I’ve got the local IP working. Then I found ngrok, and apparently I don’t need to connect via the IP. What I am trying to do is expose my website … Read more