How to customize routes?

Asked

Viewed 261 times

0

I have a controller called login and a view of the same name. When I access the login page, the URL is displayed this way: ~/Login/Login

It is possible to customize this route, so that the link stays like this: ~/Login?

1 answer

1

In this controller Login there is some Action ? Take a look at your Global.asax and see how the default route is:

IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute(
      "Default", // Padrão 
      "{controller}/{action}/{id}", // Parametros da URL
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } 

);

But in this you can put so:

new { controller = "Login", action = "NomeDaActionDentroDaControllerLogin", id = UrlParameter.Optional }

Along with everything, this way:

"Default", // Padrão 
   "{controller}/{action}/{id}", // Parametros da URL
   new { controller = "Login", action = "NomeDaActionDentroDaControllerLogin", id = UrlParameter.Optional } 

It’s okay to have a View as the same name of Controller, but look at the route pattern in this file Global.asax. And put it the way I put it here, and also check how this route is being called for example, if you are via localhost, it should appear in the default way as in the route:

http://localhost:2020/Controller/Action/Id=?....!

If any Action within the Logincontroller in your project, call is View Login with the same name, in the URL should only be called as

http://localhost:2020/Controller/Action/

It’s not called the View, is called the Action.And then the Action will render to the View that you created within her !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.