Some background info first. I have a working website in the form of a Web Forms Application created in VS 2012 Express. Then i tried adding a Web API, by adding a ASP.NET MVC 4 Web Application project and selecting Web API.
So the whole project has three different projects now.
- DAL (data access layer)
- Web Forms Application
- Web API
(I have read tutorials for adding the API to an existing project, but having the Web API in a different project is a requirement.)
To keep it simple i am only using the controllers that are included from the start until i solve this.
My problem is that i get 404 Not Found Errors when i try to run for example:
http://localhost:49919/api/Values
Now i guess this is because i am trying to access the API from my site which is another project. But even after looking for hours for solutions online i couldn't find anything that solves my problem. I found some people with the same problem, but the solutions for them didn't work for me.
The things i have tried so far is:
Added a reference to the API in the Site project.
Adding this code to the API Web.Config. Which if understand correctly should allow access between projects.
<system.webServer> <httpProtocol> <customHeaders> <clear/> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> </customHeaders > </httpProtocol > </system.webServer >
My routing in the WebApiConfig looks like this:
public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } );
Global.asax in the API looks like this:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
I tried making a whole new project with just a Web API and it worked without any editing.
This is my first time working with Web API, so i appreciate all the help i can get.
Edit: Thought i would add the purpose of the API. I will use it to get the latest messages from DAL and show it with jQuery/AJAX on the site.
Edit2: Changed the IIS port on the API to the same one as my website.
Website http://localhost:49919/
API http://localhost:49919/api/(/api because they can't have the same url)
The links look like this now 49919/api/api/values, but that can be fixed.
Ofcourse it was something as simple as that, but i had no idea that needed to be done.
{action}
. Consult the docs.