How to swap id segment position in C# MVC routes? - c#

The usual route is like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I want to have the id specified before the action so I wrote these 2 routes:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{id}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
Most of the requests work except for the following case:
Foo/2
The Index action is not called.
How can I get to the expected behavior?

Related

Defining multiple static routes and 1 dynamic route for 1 controller in .NET MVC

I have defined a couple of static routes which look like following:
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
And third is the dynamic route which looks like following:
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
I would like to have defined these two static routes for my Index controller:
/Index/Tutorials
/Index/LogOut
Every other route should point to:
/Index/{id}
The way I defined it right now works for 2 static routes, but when I try to pass parameter like this which isn't one of the two static routes like following:
http://localhost:60617/Index/12345/
Where 12345 is ID, I get following error:
The resource cannot be found.
How can I define these routes properly ? Can someone help me out ?
Here is the route class:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
routes.MapRoute(
name: "ResetPwdRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
);
}
}
The order in which you map routes are important.
Generic routes should be mapped after more specific routes to avoid route conflicts.
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "LogOutRoute",
url: "Index/LogOut",
defaults: new { controller = "Index", action = "LogOut" }
);
routes.MapRoute(
name: "Tutorials",
url: "Index/Tutorials",
defaults: new { controller = "Index", action = "Tutorials" }
);
routes.MapRoute(
name: "Index",
url: "Index/{id}",
defaults: new { controller = "Index", action = "Index" }
);
routes.MapRoute(
name: "ResetPwdRoute",
url: "User/ResetPwd/{id}",
defaults: new { controller = "User", action = "ResetPwd", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

.NET MVC Route not working propely

I have defined my routes in .NET app like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Register",
url: "Register",
defaults: new { controller = "Home", action = "Register" }
);
routes.MapRoute(
name: "Login",
url: "Login",
defaults: new { controller = "Home", action = "Login" }
);
routes.MapRoute(
name: "ResetPwd",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "ResetPwd", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
So that I can have routes like following:
mysite.com/Login
mysite.com/Register
mysite.com/ResetPwd
Now the problem arises with other controller when I try to redirect the user to dashboard when hes logged in successfully from mysite.com/Login like this:
return RedirectToAction("Index", "Dashboard");
However I'm getting an error:
The resource cannot be found.
I presume it want's me to define a route now for dashboard/index... but isn't that nuts since then I'd have to add every route manually ???
What am I doing wrong here?
Change your resetpassword route to
routes.MapRoute(
name: "ResetPwd",
url: "ResetPwd/{id}",
defaults: new { controller = "Home", action = "ResetPwd", id = UrlParameter.Optional }
);
Currently your provided url /Dashboard/Indexis looked up by this route with Dashboard being an Action on Default Home controller which doesn't exists in my opinion.

Why do ASP.NET MVC URLs need Index

Struggling to work out how to have the following
http://localhost/star/regulus
the only way it will work is to have a web address of
http://localhost/star/index/regulus
This is my 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( "", "star/{sstar}", new { Controller = "star", id = UrlParameter.Optional });
and in the Controller, I set up the View as :-
return View("~/views/star.cshtml", oStar);
so you can see I'm not using Index.cshtml
Any ideas how I get the top web address, not the second one.
You need to rearrange your routes
routes.MapRoute(
name: "StarRoute",
url: "star/{sstar}",
defaults: new { controller = "Star", action = "Star", sstar = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The default controller should usually be the last route registered
Assuming controller
public class StarController :Controller {
public ActionResult Star(string sstar){
//...other code that uses sstar parameter
return View();
}
}
The view should be located at...
~/Views/Star/Star.cshtml
...for the following URIs to work:
http://localhost/star/regulus
http://localhost/star?sstar=regulus
http://localhost/star/sirius
http://localhost/star?sstar=sirius

Default Routes not redirecting to expected page in mvc

I have a HomeController in which I have 2 ActionResults say Index and Projects. Now my RouteConfig.cs has below routes added:
routes.MapRoute(
name: "Default_1",//Changed this to some other name
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",//Made this name as Default
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Projects", id = UrlParameter.Optional }
);
So from the above configuration I was expecting by the default the user will redirected to /Home/Projects. But again it is redirecting to Index action. Why it is not redirecting to Projects action? Can't we have multiple routes here?Default name will not be considered as default url?
When you register your routes,put your specific route definition(s) before the default route definition. The order of route registration matters.
routes.MapRoute(
name: "Project",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Projects", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
With this configuration, Any requests coming will be redirected to your Home/Projects because the url pattern is still the generic pattern({controller}/{action}/{id}). So i guess you really meant something like this.
routes.MapRoute(
name: "Project",
url: "projects/{id}",
defaults: new { controller = "Home", action = "Projects", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This will send the request http://yourSite.com/Projects to Home/Projects
Or you may use attribute routing to define this route pattern in the Projects controller.To enable attribute routing, you can call the MapMvcAttributeRoutes method in the RegisterRoutes method of RouteConfig.cs. You will still keep the default route definition there.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
And in your ProjectsController
[Route("Projects/{id?}")]
public ActionResult Index(int? id)
{
//check id value and return something
}

Creating a custom route to map at the root of the website

My routeConfig.cs file currently has:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have an action in my home controller that responds at:
/home/trackUser/123
How can I modify my route config so this same action gets called at:
/trackUser.aspx?userId=123
Cannot check it right now, but try this:
routes.MapRoute(
name: "TrackUserAspx",
url: "/trackUser.aspx?userId={id}",
defaults: new { controller = "Home", action = "trackUser", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Categories