vips
vips

Reputation: 3

MVC C# Bad URL handling with multiple parameter

I am new to MVC. Actually, I am trying to handle the bad request for my application. I have added Application_Error method in Global.asax and also created a controller to handle the 404-page error. Now this works fine when my URL is like following. https://localhost:44397/t/t/t It shows proper 404 page I have set for the bad URL

But It shows again 404 page when my URL is something like following https://localhost:44397/t/t/t/e

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Can anyone help me how I can restrict above URL? Following is my route.config code.

routes.MapRoute(
           "ErrorHandler",
           "ErrorHandler/{action}/{errorMSG}/",
           new { controller = "ErrorHandler", action = "Index", errMsg = UrlParameter.Optional }
           ,
                namespaces: new[] { "MyProject.Web.Controllers" }
           );

Upvotes: 0

Views: 201

Answers (1)

Harish Mashetty
Harish Mashetty

Reputation: 433

You need to register an additional route at the end of all valid routes which would match any URL. Sample code below. This will handle all invalid requests to your application.

routes.MapRoute(
       "AllInvalidRoutes",
       "{*.}",        
       new { controller = "InvalidRoutesController", action = "Index" }           
       );

Upvotes: 1

Related Questions