SERGEY YANCHENKO
SERGEY YANCHENKO

Reputation: 43

How should I ignore the char in URL asp.net MVC?

Please, give me an advice on the following question: there's a usual string like here : /Search/Index, and a user inputs a char accidentally: /Search/Index'. How should I ignore the сhar and make a user simply follow the link Search/Index?

Upvotes: 0

Views: 212

Answers (2)

Mathias F
Mathias F

Reputation: 15901

I think this is not advisable. This would result in multiple urls for the same ressource and your searchengine ranking might suffer. It might be better to just return a 404

http://moz.com/blog/canonical-url-tag-the-most-important-advancement-in-seo-practices-since-sitemaps

Upvotes: 0

Bhavesh Kachhadiya
Bhavesh Kachhadiya

Reputation: 3962

you can add this code block in your Global.asax file.

The line "{controller}/{action*}/{id}" have {action*} so action will be index and any characters after index will consider as ignored and route to the index action.

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action*}/{id}", // URL with parameters
        new { controller = "Search", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

Upvotes: 3

Related Questions