how to change the default route in nopCommerce - asp.net-mvc

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" });

Related

ASP MVC getting GET request instead of POST

I have next specific map routes
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "MyController", action = "MyPostAction" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "MyController", action = "MyGetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
my default controller looks like
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
and the issue is next:
my get MyPageGet route show a page with include FORM with POST reqvest to MyPagePost route, but on a first call I am getting the same GET request and see in URL other extra param ?culture=de. Moreover, with or without this parameter, second call it working fine via MyPagePost route.
UPDATE:
In Chrome or fiddler Logs I see that reqvest to URL-Up/Bla-Bla has 302 status and response heared is URL-Up/Bla-Bla?culture=de. Why it can't be processed ?
just try it with
#using(Html.BeginRouteForm("MyPagePost",FormMethod.Post))
{
<input type="submit" value="Submit"/>
}
The routes in your post working for me in both html.beginform and html.beginrouteform on the first time.
i try it with the following routes and action methods
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "Home", action = "PostAction" },
new { name="[A-Za-z].+", httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "Home", action = "GetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
public ActionResult GetAction()
{
return View();
}
[HttpPost]
public ActionResult PostAction()
{
return View();
}

MVC Response.RedirectToRoute(RouteData.Values) redirects to the Area controller

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);

Pretty Paged Routing in MVC

I've already seen Paging and routing in ASP.Net MVC but I cannot get that working for me.
On my homepage I want to generate the following pretty urls for my paging:
http://mysite
http://mysite/2
http://mysite/3
Without routing the default urls generated by the pager would be:
http://mysite/?page=1
http://mysite/?page=2
http://mysite/?page=3
My RouteCollection thus far is:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"HomePaging",
"{page}",
new { controller = "Home", action = "Index" },
new { page = #"\d+" },
new[] { "MySite.Controllers" });
routes.MapRoute(
"HomePagingFirst",
"{controller}",
new { controller = "Home", action = "Index", page = 1 },
new[] { "MySite.Controllers" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "MySite.Controllers" });
}
This is generating the following routes:
http://mysite/1
http://mysite/2
http://mysite/3
Not only is this generating a non-canonical route for the first page but it is also causing all links generated like the following #Html.ActionLink("my site", "Index", "Home") to be appended with the page number of the current page.
Any idea how to do this? If you could, a brief explanation as well as an answer would be most welcome.
I eventually got it working like this. But in all honesty that was through trial and error. If someone could explain why it works I'm sure that would be very helpful to visitors.
routes.MapRouteLowercase(
"HomePaging",
"{controller}",
new { controller = "Home", action = "Index", page = UrlParameter.Optional },
new { page = #"\d+" },
new[] { "MySite.Controllers" });
routes.MapRouteLowercase(
"HomeFirstPage", // Route name
"{page}", // URL with parameters
new { controller = "Home", action = "Index", page = 1 },
new { page = #"\d+" },
new[] { "MySite.Controllers" });

How to set different Default Page in NOPCommerce2.65

I want to set different default page in nop2.65, i have register new Route
//In RouteProvider
//Custome page
routes.MapLocalizedRoute("CustomHome",
"",
new { controller = "Customer", action = "Login" },
new[] { "Nop.Web.Controllers" });
//home page
routes.MapLocalizedRoute("HomePage",
"",
new { controller = "Home", action = "Index" },
new[] { "Nop.Web.Controllers" });
it's work fine set Login Page as Default but when i click on "Home" menu it redirect login page
instead of home page.
i have also tried to set default page in Global.asax file that also not work
//In Global.asax file
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Customer", action = "Login", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
Finally I got the solution in NOP2.65 all route register under NOP.Web->Infrastructure->RouteProvider.cs. if you want to set different route then your RouteProvider.cs like
/In RouteProvider
//Custome page
routes.MapLocalizedRoute("CustomHome",
"", // Route name
new { controller = "Customer", action = "Login" },
new[] { "Nop.Web.Controllers" });
//home page
routes.MapLocalizedRoute("HomePage",
"home/", // Route name
new { controller = "Home", action = "Index" },
new[] { "Nop.Web.Controllers" });
NOTE: Set RouteName as blank for newly register route and modified HomePage RouteName.

Re-use route parameter on url

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.

Resources