0

I have a controller which have an action with a route, as shown below:

Route("{resourceType}/{id}")

my dll have some helper classes which is used by for example by another asp .net core application, now since that application also have similar routes so indirectly my controller starts conflicting with them

I solved this issue in web api, by marking my controller as internal and selecting this controller in the implementaion of IHttpControllerTypeResolver for my application.

Currently I am transferring my code to asp .net core 2.0 and I'm facing a similar problem, I tried to handle this problem using the code below -

serverInstance = new WebHostBuilder().
                UseConfiguration(config)
                .UseKestrel()
                .UseStartup<Startup>()
                .ConfigureServices(collection => {
                    // only fhir controller in cloud.
                    collection.AddMvc().ConfigureApplicationPartManager(manager =>
                    manager.ApplicationParts.Clear())
                        .ConfigureApplicationPartManager(manager => {
                            manager.ApplicationParts
                            .Add(new TypesPart(typeof(FhirController)));
                            collection.AddSingleton<IFhirConfiguration>(inputConfig);
                        })
                        .AddControllersAsServices();
                })
                .Build();

But the above code did not help, and FhirController is not detected if it is internal, if it's public it works, any idea how can I fix it?

ankush
  • 949
  • 2
  • 14
  • 33

2 Answers2

1

It sounds like you're saying you are reference one ASP.NET Core project from another ASP.NET Core project because the first has some helper classes you want to utilize in the second. If that's the case, don't do that. If both projects need access to those helper classes, factor them out into a class library that both projects can reference.

By referencing an entire web application project, you'll get too much bleed through, and problems just like what you're experiencing now.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • i understand that may be the way i am expressing issue is not very clear, but is there a way to make asp .net core use controllers which are internal like i had it in web api is the core question – ankush Feb 28 '18 at 14:40
0

FhirController is not detected if it is internal, if it's public it works, any idea how can I fix it?

Clearly, if you expect it to be utilized by others, the controller must be marked public. This workaround for the actual problem is not going to work.

The problem is that your route is too broad and will block other routes from functioning. Instead, fix your route:

Route("{resourceType}/{id}")

This route will match any 2 segment URL, such as foo/bar or abc/123.

If you expect it to work with other people's code, you need to constrain it to only the specific URLs your code uses. See Why map special routes first before common routes in asp.net mvc? for ideas on how to fix this (note that although it is for MVC 5, the same logic still applies in .NET Core).

The simplest fix is to use a literal segment to ensure it only matches when a specific string is found in the segment:

Route("MyResourceType/{id}")

If you need more flexibility, you may need to use a route constraint instead. For more advanced matching logic, you could use a custom IRouter.

NOTE: The first match wins. So, you must also ensure that your route has a chance to run by registering it before the routes of the application that is using it.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • i do not expect it to be used by others thats why i am looking for internal controller, its only used by my application – ankush Feb 28 '18 at 14:03
  • *My code is shipped as a dll to customer* - that sounds like you want others to use it. You do understand that a **DLL runs in the process that is consuming it** don't you? – NightOwl888 Feb 28 '18 at 14:04
  • may be to be more clear, my dll have some helper classes which is used by for example by another asp .net core application, now since that application also have similar routes so indirectly my controller starts conflicting with them – ankush Feb 28 '18 at 14:06