Click here to Skip to main content
15,123,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to implement a custom routing in my application where I want
after signup a user login url should be like

domainname/username/login

I try bellow code ,but not working,

C#
routes.MapRoute(
               name: "Default1",
               url: "{comp}/Login",
               defaults: new { controller = "Login", action = "Index", }
           );


please help me to this.
Posted

Because this is not the correct way to implement URL Routing in ASP.NET. When you create URLs you do so this way,

C#
routes.MapRoute(
    name: "Default1",
    url: "/{id}/{controller}/{action}",
    defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);


Since Login is controller, I have added {controller} in its place, constants are never resolved and mapped to any code-behind logic such as Controller, Action or other parameters. The parameters for controller, action or any other ID or other parameters are passed in {} blocks. You cannot pass your own variables or constant literals. If you want to, then you should accept them on your server also; such as id. It would be passed to your action as,

C#
public ActionResult Login(int id) { /* code */ }


You can pass your own variables in a very similar manner; such as username string.
   
Comments
KuntalBose 21-Apr-15 12:28pm
   
Hi Afzaal
Thanks for reply,but not working I use

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

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

and try in url with http://localhost:49767/abcd/Login but its says The resource cannot be found.any thought?
Thanks
Afzaal Ahmad Zeeshan 21-Apr-15 14:01pm
   
Can abcd be resolved to int?
I am not aware of new feature in MVC 5 Attribute Routing I found a good article here http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900