Good evening everyone.
I am in the middle phase in dotnet MVC 4
for a learning project, I created a web application mvc 4 containing an User profile.
I want to know how to customize the Routing (if that is the term to use):
For example: When a user registers or logs, it has the following link into their browser: http: // localhost / Profile / index
I would rather this: http://localhost/Profile/Username
Where Username is the name of the current user (the one that has just authetifier).
Much like facebook. Example: https://www.facebook.com/Ramses
Thanks.
Pradeep ShetPosted Dec 7, 2014, 2:55 PM
It is quite simple.
1) Go to RouteConfig.cs file which is in App_Start folder.
2) you will find default routhing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Now, here instead of id change to UserName
within the curly braces.
3) This will enable you to pass UserName as optional route data while passing the url.
4) In your Action method, pass 1 parameter say, USerName thats it.
public ActionMethod Fun(string UserName)
{
//Ur code
}
Hope this is what you expected. If answerd plz mark it as answered.
Ramsès Zogning IIPosted Dec 9, 2014, 9:13 AM
LaxmikantPosted Dec 9, 2014, 7:06 AM
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{UserName}",
defaults: new { controller = "Profile", action = "Index", UserName = UrlParameter.Optional }
check for more info on http://dotnetmentors.com/mvc/explain-asp-net-mvc-routing-with-example.aspx
Pradeep ShetPosted Dec 9, 2014, 6:30 AM
Ramsès Zogning IIPosted Dec 8, 2014, 12:25 PM
Pradeep ShetPosted Dec 8, 2014, 3:12 AM
It wont work because, when you pass the url, mvc routing engine tries to map the url structure with its table and in the first attempt itself it finds the mapping url structure, so it wont read your next entry of routing table.
Instead keep only 1 entry in routing table as both entries have same no.of sections
I mean keep only the below entry. It should work.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{UserName}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
And have you been using windows authentication, because you cant use User.Identity.Name without windows authentication. I hope you get it.
Ramsès Zogning IIPosted Dec 7, 2014, 6:45 PM
Thank You for your Answer!
This is my RouteConf.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Mapmode
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Profile",
url: "{controller}/{action}/{UserName}",
defaults: new { controller = "Profil", action = "Index", id = UrlParameter.Optional }
);
}
}
}
And this is my Controller :
[HttpGet]
public ActionResult Index(string UserName)
{
UserName = User.Identity.Name + "." + "Home";
ViewModelProfil vm = new ViewModelProfil();
vm.UserProfiles = GetUserProfiles();
vm.PictureProfils = GetPictureProfils();
vm.PersonalAccounts = GetPersonalAccounts();
return View(vm);
}
I already knew this method.
It does not work, or I me the wrong way.
I follow your instructions or not, when I hit the link: http: // localhost / Profile / Index
it shows me the user profile
when i type http: // localhost / Profile / Index / Ramses
it also shows me the user profile
when i type http: // localhost / Profile / Index / Ramsèsqdqsdsq
it always shows me the user profile
When I put a character string following http: // localhost / Profile / Index /
it always shows me the user profile
I do not know where I'm wrong.