1

I have Api Controllers and MVC controllers in my .NET CORE application.

How can I route sub domain api.mysite.com to point only on Api controllers, and dashboard.mysite.com to point on Web Application all in same project?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Softouch
  • 55
  • 8
  • 4
    Why don't you just have two different applications? – Camilo Terevinto Aug 09 '18 at 12:45
  • Have you tried anything? How about this: https://stackoverflow.com/questions/49378474/different-domain-in-the-same-app-asp-net-core-2-0 ? – spender Aug 09 '18 at 13:00
  • I didn't saw this. I tried with regular routing something, I want attribute routing, second answer looks good I think. @CamiloTerevinto Because I don't want to repeat code for repo and change in 2 apps every time. And of course I would like to be hosted as one – Softouch Aug 09 '18 at 13:06
  • Why would you have to repeat code? That's what class libraries are for. – Camilo Terevinto Aug 09 '18 at 13:07

1 Answers1

0

If you want to implement this in a single ASP.NET Core application, you can do something like this:

  1. Make Api controllers available say at path /Api. You can achieve this using routes, areas or application branches.

  2. Use a reverse proxy which is capable of URL rewriting (e.g. IIS on Win, Nginx on Linux). Configure the reverse proxy so that the requests arriving at api.mysite.com/path are forwarded to your application as /Api/path.

A remark: If you want to generate URLs in your Api controllers, you should remove the /Api prefix from the path to get correct URLs (and of course you have to configure your reverse proxy to append the necessary headers like X-Forwarded-Host, etc.) For this purpose you can use this simple middleware.

Update

As it was discussed in the comments, an application branch seems the best solution in this case because it enables separate pipelines for the MVC and API application parts.

Actually, it's very easy to define branches. All you need to do is to put a Map call at the beginning of your main pipeline in the Configure method of your Startup class:

public void Configure(IApplicationBuilder app)
{
    app.Map("/Api", BuildApiBranch);

    // middlewares for the mvc app, e.g.
    app.UseStaticFiles();

    // some other middlewares maybe...

    app.UseMvc(...);
}

private static void BuildApiBranch(IApplicationBuilder app)
{        
    // middlewares for the web api...

    app.UseMvc(...);
}

Now, when a request arrives and its path starts with /Api, the request gets "deflected" and goes through the branch pipeline (defined in BuildApiBranch method) instead of going through the main pipeline (defined in Configure method, following the Map call).

Some things to keep in mind:

  • When a request is "captured" by the branch, the prefix /Api is removed from the HttpContext.Request.Path property (and appended to HttpContext.Request.PathBase). So you need to define the API routes in the UseMvc method as if the request path had no prefix at all.

  • Using this code you have two separate pipelines but they share the components registered in Startup.ConfigureServices. If this is undesired, it's possible to create separate DI containers for each of the pipelines. However, this is a somewhat advanced topic.

Adam Simon
  • 2,762
  • 16
  • 22
  • Any chance that 2nd answer from this question works? https://stackoverflow.com/questions/49378474/different-domain-in-the-same-app-asp-net-core-2-0 I tried implementing it but it does not filter it. Thank you, If I don't manage to make it via code and routes I will do it probably in your way, /api is easier solution than reverse proxy. But proxy is great idea if I want sub domains. And I want them – Softouch Aug 09 '18 at 13:32
  • @Softouch Route constraints should work, however this can turn into a mess quickly if you have many routes. And you need to use properly configured reverse proxy to make them work, as well. IMO, **app branches** fit this scenario the best as you can configure **independent request pipelines** for your app and api. – Adam Simon Aug 09 '18 at 14:21
  • can you help me with some code example for my problem? App branches sounds good. – Softouch Aug 09 '18 at 18:16
  • @Softouch I've updated the answer, I hope this will help. – Adam Simon Aug 09 '18 at 19:50
  • With this approach I can make Token middleware only for Api? Adding it in Api branch. That's really good. – Softouch Aug 10 '18 at 08:10