I am generating scripts in process method of TagHelper class as follows
[TargetElement(\"MyTag\")]
public Class MYClass: TagHelper{
public override
I don't believe it is possible from inside the tagHelper to add script at the bottom or anywhere else but the location of the tag that the taghelper is rendering. I think if the taghelper depends on some external js file it should not be the responsibility of the taghelper itself to add the script. For example the built in validation taghelpers like:
all the validation taghelper does is decorate the span with data- attributes, it does not add any scripts to the page and the data- attributes will just be ignored if the scripts are missing.
consider that a view may have multiple validation taghelpers used and we would not want each one adding another script.
In the VS starter web app template you can see the validation scripts are added by a partial view at the bottom of the view (Login.cshtml for example)
@{await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
one possible strategy to automate script inclusion is your tagHelper could take IHttpContextAccessor in its constructor so it will be injected by DI, then you could access the HttpContext.Items collection and add a variable to indicate the need for a script, then in a partial view that adds scripts you could detect the added variable to decide which script(s) to include.
But myself I think it is more straightforward to just add the script where needed to support the use of the taghelper rather than trying to get fancy and add things automatically.
This idea only would work for external js files not for js that is written dynamically inside the taghelper, but it is better not to have such scripts and only use external script files if possible. If you really need to generate script inside the taghelper I think you will only be able to render it at the location of the element the taghelper is processing.