Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Possible duplicate of stackoverflow.com/questions/9536362/… –  Wiktor Zychla Jan 30 at 17:07
1  
I have tried the "<modules runAllManagedModulesForAllRequests="true" />" solution and it didn't do anything. I dont think his scenario is the same as mine. –  EdoBhc Jan 30 at 17:27
    
I believe the default route template for webapi doesn't contain {action}. Consult the docs. –  Wiktor Zychla Jan 30 at 18:03
1  
You are right, i changed that in the beginning. One of the included urls will look like this, localhost:49919/api/Values/GET/5. {action} is there to declare for example GET. Changing it back didn't do anything unfortunately, since my errors are coming even when trying to access just api/Values. –  EdoBhc Jan 30 at 18:26
    
@EdoBhc, could you ever solve this issue? I have exactly the same problem and Wiktor's solution didn't work for me either. I think the only difference is that you're running MVC4 while I'm running MVC5. Thanks for an answer! :) –  Luis Gouveia Mar 13 at 18:53
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.