urls on asp.net mvc 5

0

Can you leave the urls of my web application in the following way? Example:

Normal url - > localhost/MiControlador/MiAccion

Url converted - > localhost/mi-controller/mi-accion or localhost/MiController/mi-accion

MORE INFORMATION

My RouteConfig.cs

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

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}/{Size}",
            defaults: new { controller = "Signin", action = "Login", id = UrlParameter.Optional, Size = UrlParameter.Optional }
        );
    }

My Controller:

public class UserController : Controller
{
    // GET: User
    [HttpGet]
    [Route("User/l-ogin")]
    public ActionResult Login() => View();
}
    
asked by vcasas 16.05.2018 в 15:39
source

2 answers

1

One possibility would be to use the Data Annotation [Route] for this, first, you have to activate MapMvcAttributeRoutes() in the class RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {

        routes.MapMvcAttributeRoutes();

        //Rutas por defecto
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );   
    }
}

Then, it would be enough to use [Route] in the method where you want to customize your route

No parameters

[Route("~/ControladorA/el-ejemplo")]
     public ViewResult metodoEjemplo()
     {
         return View();
         //En este ejemplo la ruta sería http://localhost/ControladorA/el-ejemplo
     }

With Parameters

[Route("~/Pelicula/{idpelicula}")]
     public ViewResult metodoEjemplo(int idpelicula)
     {             
         return View();
         //En este ejemplo la ruta sería, por ejemplo  http://localhost/Pelicula/3
         //donde 3 representa el id de una película
     }

For more information on the subject you can directly see the Microsoft Documentation , since you will see different options to reach the same result, and you may find one that is more comfortable for you

Greetings

    
answered by 16.05.2018 / 15:58
source
0

You can use the attribute Route where you specify a format for your url.

For example:

public classs HomeController : Controller{
    public ActionResult Index() {
      return View();
    }
}

Normally to invoke index would be /home/index . Adding the Route attribute can change the url of that controller. For example let's say you want it to be /h-ome/index :

public classs HomeController : Controller{

   [Route("h-ome/index/")]
   public ActionResult Index() {
        return View();
   }
}

With route you can simulate subdirectories too:

[Route("home/user/start")]
public ActionResult Index()
{
  //...
}

Where the url would be /home/user/start and therefore Index would be executed.

Take a look at the documentation so you can get a good dose of the subject.

Bonus: If your action requires parameters, with Route you can make the URL more user friendly, for example, to see the user's information, normally it would be /user/info?username=einer . This can be changed to /user/einer/info :

[Route("user/{username}/info")]
public ActionResult UserInfo(string username) {
    //...
}
    
answered by 16.05.2018 в 15:56