question

LingSteven avatar image
0 Votes"
LingSteven asked Bruce-SqlWork answered

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

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

dotnet-aspnet-core-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MVC has the section construct which writes content to a section defined in the layout page. I can't add sample code to this comment but the link has sample code.



1 Vote 1 ·

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.