0

I have two projects that need to be published together on the same IIS site.

One of the projects is an Angular application, and the other is an ASP.NET Core Web API.

For various reasons, the Web API project must be the "main" one, even though it only serves requests to /api.

I thought about publishing the Angular project in an app folder, but I need requests to http://mysite/home, for example, to be handled by the Angular routes. Unfortunately, I can't use http://mysite/app/home.

A partial solution I found was to use URL rewriting in the following way, but I still haven't achieved the desired behavior, mainly due to the redirection to index.html (which shouldn't appear in the URL, obviously).

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to app folder if exists" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_URI}" pattern="^/api" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^app/{R:1}$" />
          </conditions>
          <action type="Rewrite" url="/app/{R:1}" />
        </rule>
        <rule name="Redirect to index.html if not found in app" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions logicalGrouping="MatchAll" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/api" negate="true" />
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
2
  • Can you clarify why For various reasons, the Web API project must be the "main" one,? I strongly suspect creating a web application in a folder at /api in your webroot and leaving your static site in the root would simplify things and work fine. IIS has features like <Location /> blocks in web.config or even installing ARR (Application Request Routing) to allow for more elaborate setups but your case seems simple(unless am i missing something?). Can you expand your question? Please add errors from browser or Windows Event Log, or IIS HTTPERR log. Commented Sep 21, 2024 at 9:57
  • FWIW, given no-one who knows Angular has yet answered, we have a similar setup with a React site. We get IIS/.net core to manage this in the web-site app, without a rewrite rule. We have a SpaController with [Route("{*path}", order = 5)] - the order=5 means it's applied last, if there are no other routes defined, and that controller serves an HTML page that hosts the SPA, and a ApiController with [Route("api")] which is where the API is. The ApiController is more or less just a facade for elements in an API project in same solution. YMMV. Commented Sep 22, 2024 at 0:50

1 Answer 1

0

You can publish at different host names (since https is not involved, SNI should not be/may not be used) using host headers.Therefore api.mysite.intra web site points to another IP address and app.mysite.com web site points to another IP address. Requirements for this option:

  1. You should define additional IP address for the same network card at OS level.
  2. The DNS entries should be defined.
  3. There will be two different site definitions in IIS. Each has its own binding.
  4. In this option both app & api use the same port (80). IIS differentiates requests analyzing host headers of http requests. So writing the correct FQDN URI address is essential.

Another option is to differentiate http requests using ports: Requirements for this option:

  1. You should define different ports for the sites (app.mysite.intra uses port 80, api.mysite.intra uses 81)

  2. There will be two different site definitions in IIS. Each has its own binding. There will not be any host header settings (leave empty)

  3. You can access with different names as long as ports are correct (http://api.mysite.intra:81 as an example)

Furthermore if you have IP&Domain Restrictions Module, you can access limit to the websites using source IP address. For example you can limit api access from only localhost IP addresses.

For the same site redirections, you can use HTTP Redirection Module. One of the options on the screen is:

  • Only redirect requests to content in this directory (not subdirectories):

Example: The requests for subdirectories will not be processed (redirected). Therefore http://www.abc.intra/sales will be redirected to http://new.abc.intra/default.htm but http://www.abc.intra/sales/cars/index.html will not be processes/redirected.

So if you set default http redirection from http://mysite to http://mysite/API and check the above option, this redirection will not be inherited by the subdirectories. A second redirection is for http://mysite/home - to http://mysite/app/home. This redirection can be set for inheritance or not (the above option set or Not) based on your needs.

Regards.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.