0

Let's say the main web app is: https:\mywebapp.com

Let's say I have separate Web API project and it has multiple services (api\service1, api\service2, )

For this I created 2 projects

  1. ASP.NET Core MVC Project for Main App - https:\mywebapp.com
  2. ASP.NET Core Web API project (Service 1, Service 2)

How should I set up my project and publish profile in Azure so that I have the following

  1. Main App (https:\mywebapp.com) - This is good I have publish profile and this gets published correctly in Azure
  2. Service 1 URL (https:\mywebapp.com\api\service1)
  3. Service 2 URL (https:\mywebapp.com\api\service2)

How do I achieve pt 2 and pt3 and have my web API project as a sub-app in my main web application? and share the same URL\api ?

ameya
  • 1,448
  • 1
  • 15
  • 31
  • Why don't you create single MVC app with API controller instead? – Yehor Androsov Feb 14 '20 at 20:22
  • The MVC is a main web app and I want the API projects to be as light as possible with only API controller... there is no need to initialize the API project with MVC project options... and have view specific code – ameya Feb 14 '20 at 20:35
  • what are you going to do when you will have 100 services, you are going to deploy 100 applications? i think it is preferable to scale web app until some point, and then do it horizontally to reduce executions per instance – Yehor Androsov Feb 14 '20 at 20:37
  • No... i just put an example but in general, I would rather have a separate Web App MVC project which loads the view settings and razor options during startup as the code is heavy for MVC and have another project that just has controllers... this helps in performance of the web API... i dont see a reason to mix this two... – ameya Feb 14 '20 at 20:39
  • even if you deploy 3 web apps to a single App Service plan, it will consume more CPU and RAM than if you deploy everything to a single instance. And since CPU and RAM are shared per App service plan, you gain no extra perfomance – Yehor Androsov Feb 14 '20 at 20:42
  • the only way I see to do it your way, is to host Virtual Machine, set up IIS, deploy two apps to your VM, and make IIS route bindings to your app. Azure WebApp can't help you with that – Yehor Androsov Feb 14 '20 at 20:45
  • thanks, yeah but by design and performance-wise why do you want your API project to have any view or model-specific configuration?... i modified the question based on your comment... so I want to have 1 separate projects for all API endpoints via controllers and asp.net core 3.1 provides good options to achieve that... and a separate project to the main web app... i want to have same base url – ameya Feb 14 '20 at 20:45
  • 1
    Virtual directory is what you are looking for. https://stackoverflow.com/questions/43073570/create-virtual-directory-on-same-azure-web-app – Arjun Vachhani Feb 17 '20 at 07:07

1 Answers1

2

You could create virtual directory on same azure web app.

When you first create Core MVC Project in azure webapp, go to Configuration > Path mappings and config the Virtual directory site/api.

enter image description here

When we publish the Core api to the Azure, we need to include the virtual directory path in the Site Name and Destination URL sections on the Connection tab.

enter image description here

Because Core 3.1 web api consists of one or more controller classes that derive from ControllerBase. The web API project template provides a starter controller:

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase

So, we can visit it just with controllerName. And as it is sub directory, so the Url path is api\controllerName. Here is output:

enter image description here

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • 1
    Thanks @Jaey Cai... I created the virtual directory as suggested and published Web API project. Publish was successful to https:\\mywebapp\api . But now for the main Web App... https:\\mywebapp I get the following error... HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process – ameya Feb 17 '20 at 19:25
  • Delete the whole .vs folder and restart your solution. Refer to this [thread](https://stackoverflow.com/questions/58246822/http-error-500-35-ancm-multiple-in-process-applications-in-same-process-asp-ne). – Joey Cai Feb 18 '20 at 03:04
  • Any update now? If it helps you, you could accept it as answer. – Joey Cai Feb 19 '20 at 02:06
  • nope not yet... looks like virtual directories does not work with asp.net core 3.1. i have been trying InProcess, OutOfProcess... deleting .vs folder... but with the approach mentioned above apps are published correctly but at a time only one app runs... either the main app or the sub app... i get either ANCM Mixed Hosting Models Not Supported with OutOfProcess or 500.35 ANCM Multiple In-Process Applications with InProcess – ameya Feb 19 '20 at 04:59
  • 1
    The worker process can't run multiple in-process apps in the same process. To fix it, run apps in separate application pools. – Joey Cai Feb 26 '20 at 02:12