How to serve index.html with web api selfhosted with OWIN

Move your Index.html to the root of your project. Then install-package Microsoft.Owin.StaticFiles in Package Manager Console and add the code below: public class Startup { public void Configuration(IAppBuilder app) { const string rootFolder = “.”; var fileSystem=new PhysicalFileSystem(rootFolder); var options = new FileServerOptions { EnableDefaultFiles = true, FileSystem = fileSystem }; app.UseFileServer(options); } } This … Read more

Enabling CORS through Web.config vs WebApiConfig and Controller attributes

If you add the headers to the web.config, every request that is served by that application will include the specified headers. This method is supported at the web server level and doesn’t depend on config.EnableCors() being executed. You can use that method to add any HTTP header you want. On the flip side, the EnableCors … Read more

Enable CORS for Web Api 2 and OWIN token authentication

I know your issue was solved inside comments, but I believe is important to understand what was causing it and how to resolve this entire class of problems. Looking at your code I can see you are setting the Access-Control-Allow-Origin header more than once for the Token endpoint: app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); And inside GrantResourceOwnerCredentials method: context.OwinContext.Response.Headers.Add(“Access-Control-Allow-Origin”, new[] … Read more

asp.net webapi 2 attribute routing not working

Based on your information, it looks like you are not calling the httpConfig.MapHttpAttributeRoutes() (Make sure to call this before any traditional routing registrations) Since you haven’t called MapHttpAttributeRoutes, your request seems to be matching a traditional route, for example, like api/{controller}. This will not work because routes matching traditional routes will never see controllers/actions decorated … Read more

ASP.net Web API RESTful web service + Basic authentication

Jamie Kurtze provides a good explanation of using Basic Authentication here ASP.NET Web API REST Security Basics From my understanding, if you want your requests to be stateless then each request will require the Authentication field to be set Jamie Kurtze wraps the necessary code in a class derived from DelegateHandler, while Rick Strahl checks … Read more

Using Url.Link with Attribute Routing in Webapi 2

You can use RouteName with Ur.Link when using attribute routing. public class BooksController : ApiController { [Route(“api/books/{id}”, Name=”GetBookById”)] public BookDto GetBook(int id) { // Implementation not shown… } [Route(“api/books”)] public HttpResponseMessage Post(Book book) { // Validate and add book to database (not shown) var response = Request.CreateResponse(HttpStatusCode.Created); // Generate a link to the new book … Read more

How to use Container instead of ObjectFactory in StructureMap ServiceActivator?

The static stuff is going away. If your not using a Service Locator of some type you’re going to have implement your own “ObjectFactory” as referenced here: public static class ObjectFactory { private static readonly Lazy<Container> _containerBuilder = new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication); public static IContainer Container { get { return _containerBuilder.Value; } } private static Container … Read more

How to set up Elmah with ASP.NET Web API

There are two options by using ELMAH to capture exceptions in WEB API. If you want to capture errors that are encountered in Actions and Controllers , i.e. in your business logic you can create a ActionFilterAttribute and log those exceptions to ELMAH. example: public class UnhandledExceptionFilter : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) … Read more