Get Site Hostname/Name from the startup.cs

Vote:
 

How can we get the site name from the manage website (admin tab) by using the startup.cs file 

#286758
Edited, Sep 07, 2022 9:42
Vote:
 

            var name = app.ApplicationServices.GetService<ISiteDefinitionRepository>().List().First().Name;

this probably works - untested by me 

#286759
Sep 07, 2022 10:34
Kumar - Sep 07, 2022 11:59
Thanks for the response, "ApplicationServices" giving an error

public void Configuration(IAppBuilder app)
{
var name=app.ApplicationServices.GetService
Quan Mai - Sep 07, 2022 12:12
which is the error?
Kumar - Sep 07, 2022 12:20
'IAppBuilder' does not contain a definition for 'ApplicationServices' and no accessible extension method 'ApplicationServices' accepting a first argument of type 'IAppBuilder' could be found (are you missing a using directive or an assembly reference?)
Vote:
 

It should be IApplicationBuilder

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            //other code 

            var name = app.ApplicationServices.GetService<ISiteDefinitionRepository>().List().First().Name;
        }

#286763
Sep 07, 2022 12:46
Kumar - Sep 08, 2022 8:47
This one also gives errors.
Error: The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments
Johan Petersson - Sep 28, 2022 9:42
Have you imported the namespace Microsoft.Extensions.DependencyInjection?
Vote:
 

I think the correct code would be:

var name = app.ApplicationServices.GetInstance<ISiteDefinitionRepository>().List().First().Name;
#288184
Edited, Sep 27, 2022 20:33
Quan Mai - Sep 28, 2022 10:30
I fail to spot the difference between this and the code I posted
Karol Berezicki - Sep 28, 2022 10:49
@Quan Sorry, it's my bad pasting - I've meant to use code with `GetInstance` - I've updated the answer.
However, it's basically the same as `GetRequiredService` under the hood, which doesn't make difference to your answer.
Vote:
 

Note that this will obviously just give you the first website, which is necessarily not what you want. You will need an http context to be able to resolve a website that is actually matching the current request, and this can not be done during configuration. 

#288230
Edited, Sep 28, 2022 9:40
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.