2

In my globas.asax file i have one register route

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

and in my action "BigClientLogin" it redirects to a new action called "NewLogin". So at present my current url looks like "http://localhost:65423/Authentication/NewLogin" .But i need my url in "http://localhost:65423/Login" format. To change action name from "NewLogin" to "Login" is not possible since i called this action many places in my solution. So is there any alternative solution for this in mvc routing? or is this is not possible and better will be to change my action name?

tereško
  • 58,060
  • 25
  • 98
  • 150
Nithin Paul
  • 2,169
  • 3
  • 33
  • 55

4 Answers4

3

A simple solution would be using the ActionName attribute. just put this on your action method

[ActionName("Login")]
public ActionResult NewLogin(...)
{
    ...
}

this would change only the Action Name, if you want only the path to be /login, use the Route attribute:

[Route("login", Name = "Login")]
public ActionResult NewLogin(...)
Tamim Al Manaseer
  • 3,554
  • 3
  • 24
  • 33
  • 1
    That doesn't satisfy direct access using `~/Login`. This would still need to be `~/Authentication/Login`. – Brad Christie Jan 26 '15 at 15:23
  • @BradChristie thanx, I noticed and modified the answer – Tamim Al Manaseer Jan 26 '15 at 15:25
  • 1
    Good answer assuming " i called this action many places in my solution" means "I directly call it from many places in C# code"... Renaming action will break all places where Urls are constructed via `UrlHelper.Action` (and similar) methods. – Alexei Levenkov Jan 26 '15 at 15:28
  • @TamimSalem thanks for your reply. I checked your solution, but Route and name not recognizing in my solution. Do i need to add any extra namespace for that? – Nithin Paul Jan 26 '15 at 15:30
  • @NithinPaul you only need System.Web.Mvc namespace. – Tamim Al Manaseer Jan 26 '15 at 15:42
  • @BradChristie Route and Name attributes not recognizing in my code. Do i need to add anything extra to get those attributes in my code? – Nithin Paul Jan 26 '15 at 15:43
  • 1
    @NithinPaul: What version of MVC are you running? Attribute routing is new to MVC 5 (previous versions require a NuGet package). – Brad Christie Jan 26 '15 at 15:51
  • @BradChristie My this project using mvc 3.0. May be i have to download NuGet package, thanks for pointing that. – Nithin Paul Jan 26 '15 at 15:57
  • @TamimSalem Now i got new problem i changed my route value as routes.MapRoute( "Custom", // Route name "{action}/{id}", // URL with parameters new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults ); to avoid Authentication part, But when i try to login as admin to my pages, i am getting "The resource cannot be found" because my url is http://localhost:65423/Admin/Dashboard – Nithin Paul Jan 26 '15 at 16:50
  • @BradChristie please check my above comment. Now i have this problem. So i need to alter my global.asax file? – Nithin Paul Jan 26 '15 at 16:55
2

A couple of options:

First, would be to map a route for this new login action:

routes.MapRoute(
  "NewLogin",
  "Login",
  new { controller = "Authentication", action = "NewLogin" }
);

Another option, if enabled, would be to leverage Attribute routing:

public class AuthenticationController : Controller
{
    [Route("~/Login", Name = "NewLogin")]
    public ActionResult NewLogin(...)
    {
        /* ... */
    }
}

(Just make sure routes.MapMvcAttributeRoutes() has been called in RouteConfig.cs)

With either of these, you'll have a named route you can reference in your solution (which will allow you to change it in the future if necessary):

@Html.RouteLink("Login", "NewLogin")
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Now i got new problem i changed my route value as routes.MapRoute( "Default", // Route name "{action}/{id}", // URL with parameters new { controller = "Authentication", action = "BigClientLogin", id = UrlParameter.Optional } // Parameter defaults ); to avoid Authentication part, But when i try to login as admin to my pages, i am getting "The resource cannot be found" because my url is localhost:65423/Admin/Dashboard – Nithin Paul Jan 26 '15 at 16:56
1

You can try action aliases defined as attributes, see the article for further details: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx/

[ActionName("View")]
public ActionResult ViewSomething(string id) {  
    return View();
}

The ActionNameAttribute redefines the name of this action to be “View”. Thus this method is invoked in response to requests for /home/view, but not for /home/viewsomething.

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
  • i tried your solution too, but my url now looks "http://localhost:65423/Authentication/Login. I don't want Authentication part in my URL. Thanks for your help. – Nithin Paul Jan 26 '15 at 15:38
  • 1
    Then you need to hide controller name from the route: http://stackoverflow.com/questions/3337372/asp-net-mvc-removing-controller-name-from-url – Yury Schkatula Jan 26 '15 at 15:45
0

Easy - Put this before the default route above:

routes.MapRoute(
            "BigClientLogin", // Route name
            "Login", // URL with parameters
            new { controller = "Authentication", action = "BigClientLogin" } // Parameter defaults
        );
Radu Porumb
  • 785
  • 5
  • 7