I have a dynamic list of ActionLinks generated at run time like this:
#Html.ActionLink(x.Name, "Index", "User", new { x.ID }, null)
so I'm hitting the Index method on the User controller.
I also have my RouteConfig.cs (it's an MVC4 app) set as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "Index", id = UrlParameter.Optional }
);
}
This gives me the link server/application/User/Index/1 in my list of links.
What do I need to do in the list generation and / or routing file to change this to server/application/User/1?
Add a route that doesn't need an action but won't clash with other routes
routes.MapRoute("User", "User/{id}",
new { controller = "User", action = "Index" });
Related
I have a website that need a departmentID for all sites, so i rewrote the default route to the following:
routes.MapRoute(
name: "Main",
url: "{deptID}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", deptID = "", id = UrlParameter.Optional }
);
It works fine when you for example use the following URL:
http://domain/2/Home/Index
or just simply:
http://domain/2
My problem is that when i go to "http://domain/" i get an error because i dont have specified the departmentID.
How can i add a second route which will fire when you just go to the domain.
I have tried to add the following:
routes.MapRoute(
name: "Start",
url: "/",
defaults: new { controller = "Department", action = "Select"}
);
Which doesnt work since you cant just put "/" as an URL.
I have also tried to add the default route and then just change the defaults object:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Department", action = "Select", id = UrlParameter.Optional }
);
This doesnt work either.
You have to modify your code as below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Department",
url: "{deptID}/{controller}/{action}",
defaults: new { controller = "Department", action = "Index"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Output
Let me know if the above code still not worked for you.
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 }
);
}
}
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.
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
}
Im trying to make a custom router for my mvc-learning project.
I want to keep the standrard route that directs to {controller}/{action}/{id} in case of a URL like
domain.com/user/details/72
but if i get a url like this one
domain.com/2
i want to route to a specifik controller action that takes the number as an id. So i dont want the URL to specify controller and action, cause i want the url to be real short and the controller and action should always be the same.
Ive kind of made it work but i get very strange unpredictable results so i wanted to ask if im doing something very weird. Here are my routes:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DisplayKombak",
url: "{kombakId}",
defaults: new { controller = "Kombak", action = "DisplayKombak", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Try something like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DisplayKombak",
url: "{kombakId}",
defaults: new {controller = "Kombak", action = "DisplayKombak"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
}
And KombakController.cs:
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class KombakController : Controller
{
public ActionResult DisplayKombak(string kombakId)
{
return View();
}
}
}
You may be able to force this behavior by adding route constraints.
routes.MapRoute(
name: "DisplayKombak",
url: "{kombakId}",
defaults: new { controller = "Kombak", action = "DisplayKombak", id = UrlParameter.Optional },
new {kombakId = #"\d+" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new {controller = #"\D+" }
);
The constraint on the first route will prevent the route from matching unless the first route segment is an integer, the constraint on the second route will prevent the route from matching if the first route segment is not a string of letters.