Change the name in my url of the MVC4 controller

1

I need to change my Url: localhost: 44301 / service by localhost: 44301 / product

service is the controller, I just want to change its name by product but keep calling service.

    
asked by Matias Suarez 29.11.2018 в 19:50
source

2 answers

0

There are two simple ways to achieve this:

  • Changing the name of the controller: since the framework uses the name of the controller to map the action, you can change the controller name from "Service" to "Products". Example:

    public IActionResult Productos()
    {
        ... //Método
    }
    
  • This way, your URL would be like "localhost: 44301 / Products".

  • Another way to achieve this is with route by attribute : this way it is not necessary to change the name of the method, but simply to indicate to the framework which is the route to reach that method. For example:

    [Route("nombreDelDominio/Productos")]
    public IActionResult Services()
    {
        ... //Método
    }
    
  • For more information about Attribute Routing, you can consult the following link .

        
    answered by 30.11.2018 / 14:40
    source
    2

    You could perform a redirect to the other controller, something like being

    public class ServiceController : Controller
    {
        public ActionResult Index()
        {
           return RedirectToAction("Index", "Product");
        }
    }
    

    This way when you invoke service you will go to the product controller

    If you need to work with the routing in MVC 4 you will have to apply something like the following

    routes.MapRoute(
                "service",                                           
                "service/{action}",                            
                new { controller = "Product", action = "Index" }  
            );
    

    Creating Custom Routes (C #)

        
    answered by 29.11.2018 в 22:58