How do I make my application route to mydomainname/username/controller.
I am working on asp.net mvc web application that enables a user to belong to multiple account. ie. In the application every account has its own users, and each user in one account can also be a user in another account. What i need is when a user wants to login, they specify the account they want to be logged to like this: domainname.com/accountname/login.
Am able to do this, but where am having issue is how to persist the accountname route parameter across other routes? I mean making it to be visible on the url. For now am using cookie to store and get the accountname parameter, but i need a way to make it visible on the url in every request (without having to manually route it on links) until the user singout.
Am using asp.net mvc 2
Edit: Added my route code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("", "", new { controller = "Home", action = "index" });
routes.MapRoute("", "dashboard", new { controller = "account", action = "dashboard" });
routes.MapRoute("", "contacts", new { controller = "contact", action = "index" });
routes.MapRoute("", "groups", new { controller = "group", action = "index" });
routes.MapRoute("", "sms", new { controller = "sms", action = "index" });
routes.MapRoute("", "users", new { controller = "user", action = "index" });
routes.MapRoute("", "login", new { controller = "Home", action = "login", accountUrlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/login", new { controller = "Home", action = "login" });
routes.MapRoute("", "register", new { controller = "home", action = "register" });
routes.MapRoute("", "{accountUrlName}/invitations/{ivkey}", new { controller = "home", action = "invitations" });
routes.MapRoute("", "{urlName}",
new { controller = "home", action = "index", urlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/{action}",
new { controller = "account", action = "dashboard", id = "", accountUrlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/{controller}/{action}/{id}",
new { controller = "account", action = "dashboard", id = "", accountUrlName = ""});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
So you effectively have two sets of routes here -
{AccountName}/{Controller}/{Action} and {Username}/{Controller}/{Action}.
Is this right?
It's possible for you to create these routes, but you'd have to have usernames which do not contain account names, and vice versa.
Related
I am using nopCommerce, I want to change my default route from Index to another ActionResult Promotion which is also present in same HomeController, I have done these following tricks, but no solution,
in Nop.Web\Infrastructure\RouteProvider.cs
//home page
routes.MapLocalizedRoute("HomePage",
"",
new { controller = "Home", action = "Index" },
new[] { "Nop.Web.Controllers" });
//for promotion
routes.MapLocalizedRoute("Promotion",
"",
new { controller = "Home", action = "Promotion" },
new[] { "Nop.Web.Controllers" });
in Global.asax
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Promotion", id = UrlParameter.Optional }, // changed to Promotion
new[] { "Nop.Web.Controllers" }
);
I found the solution by just adding value Home as url name in default route, I wounder I changed the Action as Index in Global.asax. This works fine.
//home page
routes.MapLocalizedRoute("HomePage",
"Home", // added value in the default route
new { controller = "Home", action = "Index" },
new[] { "Nop.Web.Controllers" });
//for promotion
routes.MapLocalizedRoute("Promotion",
"",
new { controller = "Home", action = "Promotion" },
new[] { "Nop.Web.Controllers" });
I use RedirectToRoute method to force culture in the url. All was working until I create an Admin area in my project. Now the method redirects to the Area controller.
Example with the main HomeController : /Home/Contact
public ActionResult Contact()
{
Response.RedirectToRoute(RouteData.Values);
return View();
}
The method redirects to /Admin/Home/Contact, the values of RouteData.Values before the redirection are :
[0] "Controller" Home
[1] " Action" Contact
My Main Route :
routes.MapRoute("Default_culture", "{culture}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional , new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Controllers" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Controllers" });
Route in RegisterArea method :
context.MapRoute(null, "{culture}/Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Areas.Admin.Controllers" });
context.MapRoute(null, "Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Areas.Admin.Controllers" });
I don't understand this behaviour. What am I doing wrong?
I just fixed this problem in my site.
Change Response.RedirectToRoute(RouteData.Values);
To Response.RedirectToRoute("Default", RouteData.Values);
I have routes below in my MVC4...
routes.MapRoute("Account", "Account/{action}", new { controller = "Account", action = "Index" });
routes.MapRoute("Admin", "Admin/{action}", new { controller = "Admin", action = "Index" });
routes.MapRoute("Custom", "{action}", new { controller = "Home", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
...and need to find a way to route all url which has 2 symbol(www.domain.com/word2pdf) in url to action Index and pass url path(word2pdf) as api parameter.
public ActionResult Index(string api)
{
}
Any ideas how to do that?
As #Murali said, the proper way is to use Attribute Routing, but that's a new feature for mvc5 only.
If you want or have to stay on mvc4:
routes.MapRoute(name: "SomeRoutingName",
url: "{api}",
defaults: new { controller = "SomeControllerName", action = "Index"},
constraints: new { api= ".+2.+"});
Also just for some lol's, this should also work:
routes.MapRoute(name: "SomeRoutingName",
url: "{partA}2{partB}",
defaults: new { controller = "SomeControllerName", action = "Index" });
In Controller:
public ActionResult Index(string partA, string partB)
{
var api = string.Concat(partA,"2",partB);
}
I have an ASP.Net MVC 3 application. With 2 Areas:
Auth - handles all the authentication etc
Management - area for property management
In the Management Area I have a ManagementController and a PropertyController. The ManagementController does nothing, it only has a simple Index ActionResult than return a view.
The PropertyController has an Index ActionResult that returns a view with a list of properties as well as an Edit ActionResult that takes a propertyId as parameter. In the Property Index view i have a grid with the list of properties and an option to edit the property with the following code:
#Html.ActionLink("Edit", "Edit","Property", new { id = item.PropertyId })
In theory this should redirect to the Edit ActionResult of my Property Controller,however it redirects to the Index ActionResult of my ManagementController. My ManagementAreaRegistration file looks like this:
context.MapRoute(null, "management", new { controller = "management", action = "Index" });
context.MapRoute(null, "management/properties", new { controller = "Property", action = "Index" });
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });
And my global.asax's RegisterRoutes like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
What am I missing, why would it redirect to the wrong controller and action?
Thanks in Advance!
You might need to specify the area in your route values parameter:
#Html.ActionLink("Edit", "Edit", "Property", new { id = item.PropertyID, area = "Management" }, new { })
Based on the constructor used for this call, you need to specify the last parameter, htmlAttributes, which, for your purposes, would be empty.
In your route you defined a parameter called propertyId not id:
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });
Try this:
#Html.ActionLink("Edit", "Edit","Property", new { propertyId = item.PropertyId })
I'd suggest using constraints.
For example my default route in the Global.asax is as follows:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Controllers.HomeController).Namespace });
Then in my area registration:
context.MapRoute(
"Search_default",
"Search/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Areas.Search.Controllers.HomeController).Namespace }
);
This means I get {AppName}/ as well as {AppName}/Search/Home and both work a treat.
From any Area to Home do following
Url.Action("Details", "User", new { #id = entityId, area = "" })
From Home to any Area
Url.Action("Details", "Order", new { #id = entityId, area = "Admin" })
I have a route
routes.MapRoute(
"BuildingProject",
"BuildingProject/{action}/{id}",
new {
controller = "Home",
action = "Index",
id = ""
});
i want it to behave like default route ie for url that starts with BuildingProject like http://localhost:4030/BuildingProject/DeleteAll.
I tried
routes.MapRoute(
"BuildingProject",
"BuildingProject/{action}/{id}",
new {
controller = "Home",
action = "",
id = ""
});
It worked.But on typing localhost:4030/BuildingProject it is not redirecting to it's Index but showing error.
.How to do this.
If your routes look like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"BuildingProject",
"BuildingProject/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
then http://localhost:4030/BuildingProject/DeleteAll will call the DeleteAll action on Home controller and if you navigate to http://localhost:4030/BuildingProject, the Index action will be invoked on the same controller.
Try this:
routes.MapRoute("BuildingProject", "BuildingProject/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });