It runs every time because you're running the client AND THE SERVER on your DEV machine at the same time.
The
@Console.WriteLine("...");
runs when the SERVER processes the page, NOT when the javascript runs.
You're seeing the message of that output in the Output (Debug) window in Visual Studio, correct? If it was the javascript code outputting that message, you wouldn't see it in Visual Studio.
To see the output of actual javascript code running, you would replace the above line with simply
console.log("...");
.
function test() {
console.log("test function");
}
The output of that code would be in the Console window in the BROWSER, not Visual Studio. Hit F12 in the browser to see the debug console there.