Setting app a separate Web API project and ASP.NET app
Asked Answered
V

2

3

I am trying to consume a Web API service from another ASP.NET classic project (actually it is a native JS and Html Web application). How can I configure these these two separated projects to talk with each other, on VS2012?

The final result would be:

http://localhost:8080/api/products

will return a list of products (JSON) by routing to ProductsController located on the WebAPI project,
And

http://localhost:8080/index.html

will be routed to this page on the Second project - The native HTML/JS project.

How can I configure the logical to physical Directories in these two project? Thanks!

Vasectomy answered 25/8, 2013 at 13:49 Comment(2)
So who calls the API? JS or ASP.NET Server-side code?Evenings
JS from a separate project in the solution.Vasectomy
V
8

As it turned out it's pretty simple. All I had to do is configure these both projects to share the same port on the local IIS: So for the native JS/HTML (my second project) I use:

Project for the native JS/HTML - Has nothing to do with .NET!

And the second project (WEB-API project):
enter image description here

That is it! Now the js can consume the Web-Api!

Vasectomy answered 27/8, 2013 at 14:38 Comment(0)
E
1

You just need a single project: ASP.NET Web API. I would put the index.html in the root and javascript files under scripts or js folder.

Normally static content such as js and html can be served directly by IIS in your ASP.NET Web API project. You need to make sure you add them to ignore rules in the startup:

RouteTable.Routes.IgnoreRoute("*.js");
RouteTable.Routes.IgnoreRoute("*.html");
Evenings answered 27/8, 2013 at 8:17 Comment(1)
Thanks for your replay, but you don't seem to understand my question. My first sentence had specifically mentioned I need to consume the service as if I can't have a single project (which of course can serve these purpose - this is the default..).Vasectomy

© 2022 - 2024 — McMap. All rights reserved.