How to handle invalid URLs in MVC?
For ex.: When the user enters http://localhost/User/MyProfile instead of http://localhost/User/Profile, it will throw an exception.
How to handle this request?
How to handle invalid URLs in MVC?
For ex.: When the user enters http://localhost/User/MyProfile instead of http://localhost/User/Profile, it will throw an exception.
How to handle this request?
You need first to add a custom Error page url in the web.config:
And add a controller to handle the invalid urls:
And if you want to redirect the user to the home page then you don't need the Error controller just modify the custom error tag:
Did you mean this?
I think every request should be redirected to the front controller, so wrap your code inside a try/catch block that will intercept the exception, and maybe you can redirect to the homepage in the catch block or simply raise a 404 error with an exception handler.
What is your server language ?