Replies: 2 comments 1 reply
-
hi @MJokar67, Create a controller to handle 404 requests for example NotFoundController.cc: #include <drogon/HttpSimpleController.h>
using namespace drogon;
class NotFoundController : public HttpSimpleController<NotFoundController>
{
public:
METHOD_LIST_BEGIN
// Esta macro define el método de solicitud y la ruta a la que se vincula el controlador.
// En este caso, cualquier ruta se vinculará a este controlador.
ADD_METHOD_TO(NotFoundController::handleNotFound, "/", Get);
METHOD_LIST_END
void handleNotFound(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
// Aquí puedes personalizar la respuesta 404
HttpResponsePtr resp = HttpResponse::newNotFoundResponse();
resp->setBody("<h1>404 Not Found</h1>");
callback(resp);
}
}; In the config.json of your project add follow configuration for controller path: {
"http": {
"routers": [
{
"name": "api",
"useFilter": true,
"filters": [],
"routes": [
{
"path": "/(.*)",
"controller": "NotFoundController"
}
]
}
]
}
} The above configuration states that any request matching the regular expression /(.*) will be sent to the NotFoundController. Make sure the path for the controller matches the setting in the NotFoundController::handleNotFound controller. I hope it can help you as a guide. Regards, palonza |
Beta Was this translation helpful? Give feedback.
1 reply
-
/// Set custom 404 page
/**
* @param resp is the object set to 404 response
* After calling this method, the resp object is returned
* by the HttpResponse::newNotFoundResponse() method.
* @param set404 if true, the status code of the resp will
* be set to 404 automatically
*/
virtual HttpAppFramework &setCustom404Page(const HttpResponsePtr &resp,
bool set404 = true) = 0; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
MJokar67
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to apply a custom general "Not Found" filter that will pass the filter if the path, page, method, or file is not found.
Beta Was this translation helpful? Give feedback.
All reactions