C # MVC 5 RouteConfig redirection

Recently, I had to update the mvc web application so that the base system object was displayed in the user interface with a different literal.

Let's say

I used to have: "Ship"

Now I am asked to do this: “Ships”

URLs displayed by agreement: mysite/{controller}/{action}/{id}

So, I had these URLs:

mysite/Vessels/Record/1023 

mysite/Vessels/CreateVessel 

I did all the renaming in the user interface, so that the names and labels were changed from ship to ship, and now I asked to take care of the addresses as well.

Now I don’t want to rename the names of the Controllermethods or the names ActionResultbecause it is a rather difficult refactoring, and because it is very likely that the literals will have to change again soon ...; -)

- , RouteConfig - , ?

+4
1

, , VesselsController:

public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Vessels",                                              // Route name
                "Ship/{action}Ship/{id}",                               // URL with parameters
                new { controller = "Vessel", id = "" }                  // Parameter defaults
            );

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

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

, . , , ShipController.

+1

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


All Articles