Setting app a separate Web API project and ASP.NET app - asp.net

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!

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:
And the second project (WEB-API project):
That is it!
Now the js can consume the Web-Api!

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");

Related

Integrate Angular JS project into Web Api project

I currently have a Play project where Angular Front-end is integrated into it with Gulp. Now I need to re-use the angular code into a .Net Web-Api project. Having APP and API as separate project will work. But to avoid dealing with different ports and CORS only option is to have one project that is deployed to one port where AngularJs project gets integrated along with Web Api project.
I have checked many question only this Stack-overflow answer seems relatable to my scenario but no luck with that solution too.
Does anyone know how to do that kind of integration. And is it possible to have integrated both into one project
Create ASP.NET MVC application.
Change default Index.cshtml with your index.html from Angular application. (You'll maybe need to correct paths to other files).
Now ASP.NET runs your application so you can add WebAPI Controllers instead of MVC Controllers inherit from ApiController.

Implementing Angular 2 with Asp.Net Web Api

Could anybody please help me out regarding how to implement Angular 2 with Asp.Net Web API? Any tutorial link would be helpful.
There is very little to no documentation available in this regard.
I did implement the https://angular.io/docs/js/latest/quickstart.html
But it requires npm start which starts the node server useful for CommonJS module loading. I want to use IIS and Web API. Can I use CommonJS or do I have to use any other module system
If I use CommonJS with IIS development server I get this error:
in the console. The same link works fine when I start using Node server. This means on the production server I have to use Node as well as IIS for Web API? Is there any way I can use Angular 2 only with IIS and possibly eliminate CommonJS if needed. Any tutorial to Angular 2 with Asp.Net would be helpful.
You have to use SystemJS as your module system, if you want to use it exactly like in the tutorial. If you want to use CommonJS, use something like Browserify.
You can have a look to following article for Angular 2 and Asp.net MVC with Web API.
https://www.codeproject.com/Articles/1181888/Angular-in-ASP-NET-MVC-Web-API
It's a good one but this article uses Asp.Net MVC in the middle.
You might consider to remove Asp.net mvc part and develop a website with Angular(client side) and web api(server side).
This one is a simple example.
https://github.com/thinktecture/apisummit-2016-angular2-webapi
On the other hand, if you want to use IIS then you need the build your angular project with
ng build --prod
and move the dist folder into your IIS website folder. Note that you don't need a node server on IIS because nb build transpiles old the typescript files into javascript and gets the other static files(html, css, js, etc...) and prepares for you to deploy to any web server like IIS.
another sample angular(client side)-Asp.Net Web API(server side)
https://www.youtube.com/watch?v=AYgs0kLjTLE

Serve Static Files outside MVC pipeline

I'm attempting to build a single page application using AngularJS on the client side and asp.net with Web API on the backend. I've been utilizing a complex client side build pipeline using Grunt. Long story short, this tool provides me a dist folder which contains all of my scripts/styles etc and is minified, uglified and all that good stuff.
At this point, I have no need for the server side MVC pipeline when it comes to URL routing and view rendering. I've placed the dist package at siteroot/Content/dist. Ultimately, my goal is to statically serve the files located there and leave the rest of the application intact. However, I've found no way to be able to do this. I'm forced to navigate to mysite.com/content/dist in order to actually see my application. I discovered virtual applications and directories and have attempted to remap the application at / to site\wwwroot\Content\dist. This has the intended effect in that now navigating to the root of my site serves the HTML files I'm looking for, however it simultaneously seems to break all of my Web API endpoints as it appears that the application is running out of a different path entirely.
Is there a way that I can have the best of both worlds? I want my API endpoints to exist as they do now but simply serve my site statically out of \Content\dist
You can use the StaticFiles middleware and set your dist folder as the root. I have an example how to set this up in a console application, but all you would need in an existing web app is to have a Startup class to configure in the middleware.

can I add the Web API folders to an existing project?

Microsoft has a great tutorial here for learning how to build Web API web apps in Visual Studio 2013:
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
in it, he shows you how to include the Web API folders and references:
...is there a way to add these to a previous ASP.NET Webforms project that didnt have that checkbox selected? I'm working on an existing app and want to add Web API functionality to it.
thanks
yes -- there is a tutorial for adding Web API to an existing ASP.NET Webforms project here:
http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-aspnet-web-forms
...it doesn't create the Models, Controllers, or App_Start folders, but i added them manually and placed my files from another stand-alone project there. once adjusting the namespaces it all operates properly.

writing azure-friendly asp.net

I'm building an asp.net application that will be deployed on Azure. For the moment, I'm using regular asp.net: I'm building it in VS.
I have a web service that's in an asxm file MyService.asmx with a code behind file MyService.cs that calls a different class library called MyBigClass.cs that does most of the work.
If I want to prepare for a deployment on Azure in a few months and make that web service work in its own web role that I can scale based on usage load, should I put the classes that are in the MyBigClass.cs file into the MyService.cs file? The MyBigClass.cs file contains classes that are used for other parts of the application as well; should I remove these classes and only include the ones used by the web service?
Thanks.
Difficult to give a specific answer, and to be honest - I don't think the answer to this would be Windows-Azure-specific, but rather - this is simply a design best practice question, isn't it?
It comes down to how you will go about maintaining the application, and how you are going to manage versioning, but generally speaking - code that isn't shared does not need to get deployed to both roles, so either move it back with the 'parnet' solution (web app or service), or keep in a separate assembly which you will deploy with only the relevant role.
Code that is shared between the web app and service will exist in a shared assembly which you will deploy into both roles.
So you might end up with the following projects
A web site
An assembly supporting the web site
A Web service Service
An assembly supporting the web service
A shared assembly between the web site and web service
I hope this makes sense

Resources