Twitter-like routes in ASP.NET MVC 2 application - c#

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.

Related

Take string input on default index method in mvc5

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

How to route from one controller action method to another controller action method without displaying controller name in MVC 5

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

Redirect to action based on role not working. Maybe routing needed?

I have the following controller
public class GlobalAdminController : Controller
{
// GET: GlobalAdmin
[AuthorizeUser(Roles = "admin")]
public ActionResult Index()
{
return View();
}
}
And the home controller which is the main landing page for the app
public ActionResult Index()
{
if(User.IsInRole("admin"))
{
RedirectToAction("Index", "GlobalAdmin");
}
return View();
}
The redirect to action is executed in the debugger
However the index action itself is not being executed on the global admin
I wonder if there is a better way to do this? maybe throught routing?
You have to use return to provide the redirect informations (url) to the browser. And the browser will redirect to the new location.
public ActionResult Index()
{
if(User.IsInRole("admin"))
{
return RedirectToAction("Index", "GlobalAdmin");
}
return View();
}
The point of the routes is to provide a way to access the actions from controller. Instead of accesing GlobalAdmin/Index you use the route to provide another way like admin/index
routes.MapRoute(
name: "Default",
url: "admin/{action}/{id}",
defaults: new { controller = "GlobalAdmin", action = "Index", id = UrlParameter.Optional } );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

Change nopcommerce default action

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

controller's action parameter doesn't accept /

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

Categories