How to change nopcommerce default action?
I create new action in HomeController, and want to be default page.
I change:
routes.MapRoute(
"",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
To:
routes.MapRoute(
"",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "NewAction", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
But nothing has changed.
When you navigate to /Home/Index, MVC parses route as follows:
Controller: Home
Action: Index
Id:
If you navigate to /Home:
Controller: Home
Action: NewAction (from route's default action)
Id:
You can make it always activate NewAction like this:
routes.MapRoute(
"",
"{controller}/{id}", // URL with parameters
new { controller = "Home", action = "NewAction", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
);
You can try like this.
//your default action
public ActionResult Index()
{
return RedirectToAction("NewAction"); //Like Response.Redirect() in Asp.Net WebForm
}
//your new action
public ActionResult NewAction()
{
//some code here
return view();
}
Related
i want to take input of username value on default mvc5 home page controller Index method. I already tried to do it like bellow [Route("{Username?}")] but it suppose to get value on url: https://localhost:44330/myusername but this url not hits the Index method. Can i do it without changing in RouteConfig.cs or how can i do it changing RouteConfig.cs even?
public class HomeController : Controller
{
[Route("{Username?}")]
public ActionResult Index(string Username)
{
return View();
}
}
RouteConfig.cs code:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can easily do it from RouteConfig.cs. Example codes are bellow:
routes.MapRoute(
name: "Default",
url: "{username}",
defaults: new { controller = "Home", action = "Index", username = UrlParameter.Optional },
namespaces: new[] { "ProjectNameSpace.Controllers" }
);
I have two different MapRoutes in Route.config as follows...
routes.MapRoute(
name: "Default",
url: "{action}",
defaults: new { controller = "Index",action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "SubjectRoute",
url: "{action}",
defaults: new { controller = "Subjects", action = "Subjects" }
);
and my #HTML.ActionLink is as follows in Index action method which is in IndexController
#Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { #class = "text" })
Now when I click on SUBJECTS link in Index action method, it should go to Subjects action in Subjects controller with out displaying controller name in the URL.
How can this be done?
routes.MapRoute(
name: "SubjectRoute",
url: "Subjects",
defaults: new { controller = "Subjects", action = "Subjects" }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Index",action = "Index", id = UrlParameter.Optional }
);
With the above, calls to
#Html.ActionLink("SUBJECTS", "Subjects","Subjects", new { }, new { #class = "text" })
should generate
/Subjects
The default and more general route will now catch all other requests and route them to the IndexControllerwhich now acts as the site root/index.
for example assuming
public class IndexController : Controller {
public ActionResult Index() {...}
public ActionResult ContactUs() {...}
public ActionResult About() {...}
}
the available routes based on above controller are
/
/Index
/ContactUs
/About
I am trying to remove area name and controller name from URL.I am able to remove area name. But if trying to remove controller name then "Page not found error" displayed. Below is the code snippet which I have used to remove the area and controller name.
public class HomeAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Home";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
//context.MapRoute(
// "Home_default",
// "Home/{controller}/{action}/{id}",
// new { action = "Index", id = UrlParameter.Optional }
//);
//context.MapRoute(
// "Home_default",
// "{controller}/{action}/{id}",
// new { action = "Index", id = UrlParameter.Optional },
// new { controller = "(Home)" }
//);
context.MapRoute(
"Home_default",
"{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "(Home)" }
);
}
}
In the registerarea function the first two routes(commented) are working perfectly. if I use the first one the URL comes with Area/Controller/Action. If I use second one the area is not coming in URL.The URL come ups with Controller/Action.
In the third one I am trying to remove both area and Controller
Is any thing wrong in my third route. Please suggest
Have you tried this:
routes.MapRoute("default", "{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional});
I have controller's action
public string Index(string id)
{
return id;
}
I have only this route in Global.asax.cs
routes.MapRoute(
"Default",
"{id}",
new { controller = "Start", action = "Index", id = UrlParameter.Optional }
);
For url like this "http://localhost/stuff" and "http://localhost/hello" it works. But it doesn't work for url like "http://localhost/stuff/add". How can I fix it?
Add wildcard (asterisk) before the id:
routes.MapRoute(
"Default",
"{*id}",
new {controller = "Start", action = "Index", id = UrlParameter.Optional}
);
I'd like to have routes set up as follows:
xyz/ maps to one action method with no parameters, but xyz/{username} maps to a different action (in the same controller or not, doesn't matter) that takes an argument called username of type string. Here are my routes so far:
routes.MapRoute(
"Me",
"Profile",
new { controller = "Profile", action = "Me" }
);
routes.MapRoute(
"Profile",
"Profile/{username}",
new { controller = "Profile", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Currently, if I navigate to /Profile/someuser, the Index action in the Profile controller is hit as expected. But if I navigate to /Profile/ or /Profile, I get 404.
What gives?
It works fine for me. I suppose you do not have a Me action method in your Profile controller?
I tried with the following routes:
routes.MapRoute(
"Me",
"Profile",
new { controller = "Home", action = "Me" }
);
routes.MapRoute(
"Profile",
"Profile/{username}",
new { controller = "Home", action = "Index" }
);
With the Index and Me action methods defined like this :
public ActionResult Index(string username) {
ViewData["Message"] = username;
return View();
}
public ActionResult Me() {
ViewData["Message"] = "this is me!";
return View( "Index" );
}
I used the default Home/Index view.