Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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 '14 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. – Eldin Behlic Jan 30 '14 at 17:27
    
I believe the default route template for webapi doesn't contain {action}. Consult the docs. – Wiktor Zychla Jan 30 '14 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. – Eldin Behlic Jan 30 '14 at 18:26
2  
@LuisGouveia Yes, like i wrote in the edit i changed the IIS port in the settings for the api project so it was the same as the main project. That solved it for me. Sorry for taking so long to respond, didnt see your post before now. Good luck! – Eldin Behlic Mar 16 '14 at 0:44

1 Answer 1

up vote 1 down vote accepted

The solution i used for my problem:

I changed the IIS port on the API project to the same one as my website project.

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.

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.