Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

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
Vote:
 

Just to echo Johan here, startup.cs is the configuration of the site, not the routing and delivery of a site's content.

What problem are you trying to solve by getting a site name during the startup.cs?

#288629
Oct 04, 2022 13:20
Vote:
 

Hey Kumar, Could you please explain why you want the Sitename in startup.cs file?

#288723
Oct 06, 2022 7:04
* 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.