MVC routes. Avoid multiple URLs for the same location.

The default route in ASP.net MVC is as follows:

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

This means that I can achieve the HomeController / Index action method in several ways:

http://localhost/home/index
http://localhost/home/
http://localhost/

How can I avoid three urls for the same action?

+3
source share
3 answers

If you want only:

http://localhost/

then

routes.MapRoute(
    "Default",
    "",
    new { controller = "Home", action = "Index" }
);

If you want only:

http://localhost/home/

then

routes.MapRoute(
    "Default",
    "home",
    new { controller = "Home", action = "Index" }
);

and if you want:

http://localhost/home/index

then

routes.MapRoute(
    "Default",
    "home/index",
    new { controller = "Home", action = "Index" }
);
+4
source

You see the default values.

Get rid of the default values ​​for the controller and actions.

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

This will result in the type of user in the controller and action.

+1
source

, SEO? Google , .

, - . <head> . , <link href="http://www.mysite.com" ref="canonical" /> Views/Home/Index.aspx URL-, , SEO URL-, .

:

SEO ,

0

Source: https://habr.com/ru/post/1795323/


All Articles