Server.on definition change when Wifi.mode is changed

I have a problem to change a definition how client is handled.
First in AP mode :
WiFi.mode(WIFI_AP);
WiFi.softAP(“Test AP”, “”);
server.on("/", handle_AP_OnConnect);
server.on("/setting", handle_AP_Setting);
server.begin();
then I will change mode to STA and change definitions :
WiFi.mode(WIFI_STA);
server.stop();
WiFi.begin(esid.c_str(), epass.c_str());
while (WiFi.status() != WL_CONNECTED){
server.on("/", handle_OnConnect);
server.begin();

but when client is connected, always will receive a web page from “handle_AP_OnConnect”
What’s wrong or how to modify correctly ?
Thanks
Alex

What platform should that run on?

For ESP32: Calling the on() method on the same object again with the same URL path does not overwrite the previous handler but adds another one which will never be called. The library doesn’t include functionality to remove a handler (yet).

Yes, ESP.

There is no platform ESP, on the Arduino IDE the most common platforms from Espressif are ESP8266 and ESP32.

Did you read the rest of my reply?

You could do this once:

server.on("/", commonFunction);

And store a function pointer that would be called from commonFunction(). It would point to either handle_AP_OnConnect() or handle_OnConnect()

Very good idea. I implemented this way. Thank you.