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

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,316 Reputation points Volunteer Moderator
    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.