Run Angular and ASP.NET Web API on the same port - asp.net

I am currently using angular to issue API call to an API server running ASP.NET. However, I have a cross-origin issue as for angular development, I am using localhost. While in the production version they will all run under the same domain using IIS.
Is there a way to run the angular app on the same port with ASP.NET?
P.S.: I am also open for other alternatives on solving this issue.

I've encountered the same problem, then I've found this post on medium, hope this works for you.
Edit:
Actually the solution that I've used is from that article.
The idea is that you can't "publish" the API and the Angular app on the same port, but you can use a proxy to connect from the Angular app to the API on the other port.
Update: sorry for not answering this long time ago.
To deal with the cors issue (in this case) you can use a proxy with the ng serve command.
For example, in my case I have used a proxy.conf.json file like this:
{
"/api/*": {
"target": "http://localhost:3000",
"secure": false,
"pathRewrite": {"^/api" : ""}
}
}
This code rewrite the url on every request to /api/* to the http://localhost:3000 where your api is listening.
So, to illustrate, if in angular you make a request like http://localhost:4200/api/users it will be redirected/rewrited to http://localhost:3000/api/users solving the cors issue.
Now, the way you have to run your angular application is different.
ng serve --proxy-config proxy.conf.json

I was able to achieve that with IIS successfully! I know the post is old but hopefully, it will save time for solution seekers :)
First (just a reminder) ensure that you have .NET Core Hosting Bundle installed on IIS machine (link could be found here). Bear in mind that it will require at least WinSrvr2012R2 to run.
Now copy published .net core API solution folder to the server. The same for Angular - next reminder here: execute ng build --prod then copy dist folder to the server.
Then configure IIS - create a new web site that points to the Angular app folder. Your Angular app should run at this point (but obviously there is no API yet).
Go to application pools - you will see the pool created for your application. Open basic settings and change CLR version to 'No managed code'.
And finally, click on your Web Site and 'Add application' under it. Point to dotnet core API folder and name it using some short alias. Now you should have a website structure with the application included.
If your angular app URL is:
https://myiissrvr/
your API is under:
https://myiissrvr/[ALIAS]/
DONE
Final remarks:
Usually, web API using URL structure like
https://myiissrvr/api/[controller]/[action]
So after bundling it together, it will look like:
https://myiissrvr/[ALIAS]/api/[controller]/[action]
With that approach, you should be able to attach multiple web API services under statically served Angular website - each one under its own alias. Potentially it might be useful in many scenarios.

Related

Understanding Blazor hosting

Disclaimer
I am referring to Blazor hosted , not server-side !
Can someone please explain me when running browser-hosted Blazor , why do i need to place in the Server.Startup the extension app.UseBlazor<Client.Startup> ?
If the Blazor client is already hosted in a net core project -> Client.Startup why does my Server project need to add this extension ?
Isn't the client supposed to be decoupled from the server?
I am missing something here ? Why does my Server project need to know about the Client since my Client project is already standalone ?
I have added a picture.
So as you can see in my picture , if the server is communicating via http with the client, why do i need to add the extension ?
Blazor is already running in a asp-net-core which is the Client project am i not right?
"Why does my Server project need to know about the Client since my Client project is already standalone ?"
No, yours is not a standalone project. Your project contains two apps. Just read what you wrote yourself. Do try to remove your server project and run the Blazor app. You've got a front-end app (Blazor) running in the Browser. But you need an app to serve the Blazor app: ASP.NET Core app that runs on a server.
"Blazor is already running in a asp-net-core which is the Client project am i not right?"
As far as I know, Blazor is running under mono webassembly.
I would suggest you to read the contents of the UseBlazor method to learn what it does, and why it is needed. And as far as I recall it's Client.Program rather than Client.Startup.

Deploying asp.net web api to azure app service

Problem: after deploying my asp.net core web api to azure app service I can't get a response from the expected endpoint
Steps followed:
I have an api that works fine when serving from my local machine, developed using Visual Studio Code. (The code for that is here https://github.com/samrae7/blog-api FYI)
I followed the instructions in this video: https://www.youtube.com/watch?v=C8J_CRy2_XA to push the files up to Azure App services. Basically I used the VS Code azure app service extension to create a webapp on azure and push my files.
I can see my files have been pushed to Azure ( by logging in to the portal and looking) but when I navigate to the expected URL of my api, http://sams-blog-api.azurewebsites.net, I get 'site not found'. If I try to send a request to the expected endpoint (I append /api/resource as this is the path of the endpoints on my local machine) I get '404 not found'
So my question is what do I have to do to actually get my api up and running online after pushing the files to azure app service
Thanks
Your website is not accessible due to missing DNS entry. I check it using the mxtool
https://mxtoolbox.com/SuperTool.aspx?action=a%3asams-blog-api.azurewebsites.net&run=toolpage#
Please ensure that your url is correct.
The basic answer to this question was that I had not understood that you have to create a Database in azure for your deployed app to connect to, and you need to set the connection string as an env variable in azure so that it knows where to look, and you should also add code that automatically updates the database on launch.
This tutorial was what helped with that: https://learn.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-dotnetcore-sqldb
I then hit another issue where the deployed app was still returning a generic 500 server error. By enabling 'Diagnostic logs' via the azure portal, and downloading the logs, I saw this detailed error: The configuration section 'httpProtocol' cannot be read because it is missing a section declaration referring to a piece of config in my Web.config. I deleted it (I don't need it for now) and that fixed the issue

IIS not working with multiple Asp Net MVC apps - Error 404 - React JS

I am having a problem to access my application when I put it inside a website in IIS.
The application works if I publish it as a website. As you can see in the screenshot below, the application is a website. I can access it by just typing the Server IP Address in the browser.
However, I need my server to host multiple applications. This server that I am testing is a copy of another server that hosts multiple asp net applications. The difference is that the applications that this other server hosts are not MVC based. Therefore, I have it working for the old application I have.
I have this new project that I've been developing using ASP Net MVC. My guess is that there is a problem with routing althought I've tried all of these suggestions already:
MVC3 routing not working after deployment on IIS7.5
https://weblog.west-wind.com/posts/2011/Mar/27/ASPNET-Routing-not-working-on-IIS-70
IIS7 MVC Routing Not Working
What is need is something like:
http://ServerIPAddress/Application1
http://ServerIPAddress/Application2
The scenario bellow is what I am aiming for:
Finally, here is the error I am getting. Weird thing is that the website starts to load some things.
UPDATE:
I forgot to mention I am using React JS in my application. I found the problem was related to the router.
Which folder are you choosing while hosting ?
Try selecting project folder which is with .sln file & packages folder.
Well, I am using react Js here, and after some research I found that this problem would be related to the Router.
In order to solve this problem, here is what I did:
1 - In the app.jsx (react uses this file as a router) file I changed:
<Router history={browserHistory}>
to
<Router history={useBasename(() => browserHistory)({ basename: Package.baseName})}>
2 - I referenced my package.json file:
import Package from '../package.json'
3 - Opened my package.json file and added the property baseName:
"baseName":"/nameOfTheFolderInIIS"
After doing that, I was able to connect to my application.
https://localhost:80/nameOfTheFolderInIIS/login

gruntfile.js issues or minification issues

We are working on developing a website using different APIs naming Firebase, Filepicker etc. Technologies used are Node.js and AngularJS. The project is based on a tool called yeoman, which is a scaffolding tool for web apps. We keep our modules updated through bower and NPM. The website is developed and running fine locally. We are inclined to host the app on Heroku's server and whenever we push the production code the application breaks without any message and the server crashes. I believe there are some issues with Gruntfile.js or minification issues with angular when used with grunt. Please connect me with someone who could guide me to setup the website on Heroku's server. We use heroku toolbelt for windows to push the changes.
There are some known issues with minifying AngularJS app, some of the declarations need to be written in particular way. See this SO answer.
Also, read the docs, scroll down to the part "A note on minification".
I've found this when looking for Heroku + AngularJS issues.
Please also verify compression server settings you are using.

Change deploy location for solution published using web deploy

For a long time I have been performing dated deploys to our IIS servers, basically this means that each deploy gets its own dated folder within the web site folder (c:\sites\my-site\20140824-1236 for example). I then redirect the web app to the new deployment folder.
This gives us a quick role back capability if things go pear shaped.
The problem is that we are looking at moving deployment over to the new web deploy techniques, while we have been using the new techniques within QA and UAT for a while - the standard behaviour is to simply replace the files at the location defined within the IIS web site.
Before I go and start looking at remote PowerShell to re-configure IIS before executing the web deploy.. does anyone know how I can achieve this using standard web deploy.. maybe with msbuild extensions or something?
How about using the automatic backup capabilities in Web Deploy v3?
http://www.iis.net/learn/publish/using-web-deploy/web-deploy-automatic-backups

Resources