Tag: Web API 2
-
Dynamic Robots.txt with Web Api 2
Hi everyone, For a project I’m currently working on I needed a dynamic robots.txt. Because our test environment is public facing we want to keep it from being indexed by Google etc. It took a bit of Googling to find a solution that worked, but in the end it was actually pretty simple. Here’s the…
-
AWS EC2 Elastic Beanstalk Going to Sleep – .Net/Windows/MSSQL Server Express
Hi everyone, I’ve been having a bit of an issue with my AWS app going to sleep and taking a long time to handle initial requests. I’m using .NET with Elastic Beanstalk on a T2 Micro Instance and MSSQL Server Express on RDS. My FrontEnd is a static ReactJS app that sits in S3 behind…
-
Web API 2 – ExceptionMessage=No MediaTypeFormatter is available to read an object of type ‘HttpPostedFileBase’ from content with media type ‘multipart/form-data’.
Hi everyone, I ran into the following error while trying to get image uploads working with Web API 2: ExceptionMessage=No MediaTypeFormatter is available to read an object of type ‘HttpPostedFileBase’ from content with media type ‘multipart/form-data’. I had been trying to copy the following from an mvc controller in another project: public IHttpActionResult Upload(HttpPostedFileBase file,…
-
DbSet does not contain a definition for ‘FromSQL’ and no extension method ‘FromSql’ accepting an argument of type ‘DbSet’ could be found.
Hi everyone, I ran into the following error while attempting to use a custom query with EntityFramework: DbSet does not contain a definition for ‘FromSQL’ and no extension method ‘FromSql’ accepting an argument of type ‘DbSet’ could be found. This one’s pretty straight forward: // Install the following package via nuget Install-package Microsoft.EntityFrameworkCore.Relational //Add the…
-
Include UserId in Login Response (Token) – Web API 2
Hi everyone, A quick post on how to include the user’s id in your login response when using Web API 2. The default response to the /Token request is as follows: { “access_token”: “xxxxxxxxxxxxx_xxxx”, “token_type”: “bearer”, “expires_in”: 1209599, “userName”: “test@test.com”, “.issued”: “Mon, 23 Apr 2018 06:08:03 GMT”, “.expires”: “Mon, 07 May 2018 06:08:03 GMT” }…
-
Cannot attach the file ‘C:…database.mdf’ as database x – Entity Framework
Hi everyone, I ran into the following error when attempting to run ‘update-database’ on an initial migration: Cannot attach the file ‘C:Users…App_Dataaspnet-…115933.mdf’ as database ‘aspnet-…15933’ The solution to this one is pretty easy, remove the initial catalog property from your connection string. …33.mdf;Initial Catalog=aspnet… This is apparently caused by issues with EntityFramework and multiple projects…
-
Retrieving User Id in Web API 2 Controller – .NET
Hi everyone, Just a quick post on how to retrieve the current user’s id in a Web API 2 controller: var userId = RequestContext.Principal.Identity.GetUserId(); Note that you’ll need the following using statements: using Microsoft.AspNet.Identity; using System.Web.Http; Thanks to the following stackoverflow post for the info: https://stackoverflow.com/a/21618056/522859