What's the similar way in asp.net core mvc like ClientScript.RegisterStartupScript in asp.net webform

Xie Steven 831 Reputation points
2021-07-28T08:53:31.493+00:00

Hello,

I'm porting the old ASP.NET webForm project to ASP.NET Core MVC. I'm now cannot find the proper way to do the same work like ClientScript.RegisterStartupScript() method in ASP.NET.

My target is to execute JS method when the page finishes loading but before the page's OnLoad event is raised.

Old code: ClientScript.RegisterStartupScript(this.GetType(), "windowLoad()", "<script language='JavaScript'>windowLoad()</script>");

if there're alternative ways to achieve it, please give me some code samples.

Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,551 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 64,826 Reputation points
    2021-07-28T15:04:46.317+00:00

    In mvc instead of using the code behind (action), typically you would add the script in the view. You might place an indicator in model or view bag.

    Also as it now common to add library scripts at the end of the html rather than head, the layout typically defines a scripts section that view can use to have the script render after the library includes.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.