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.
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.
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 .
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" }
);