I want to insert a route into the route table but can't work out how to do it.
For example I have a route mapped as below:
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However I want to insert that same route into the route table using the syntax below. How do I do it?
RouteTable.Routes.Insert(0, new Route(
Thanks for your help.
After a bit of thinking I found the way to do it:
Route myRoute = new Route("", new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" }, {"id", UrlParameter.Optional }}, new MvcRouteHandler());
RouteTable.Routes.Insert(0, myRoute);
The routes you have there is the RouteTable.Routes and by MapRoute this is being added to the tables. You can only add each route once.
Related
Let say I have a website www.example.com
the default routing looks like
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
Ok that works fine but let's say I want my site when I go to www.example.com/id to go to www.example.com/login/index/id
How would I configure/add routing for this, without breaking my other pages where I am actually trying to go to www.example.com/controller?
EDIT: Unfortunately id is a string so I do not have any concrete constraints that I can think of that would work. Think of maybe instead of the id I should have said companyname or sitename so the URL would look like www.example.com/companyname .
The only solution that I have come up with so far is adding a maproute for each one of my controllers like this
routes.MapRoute(
name: "Home",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Settings",
url: "Settings/{action}/{id}",
defaults: new { controller = "Settings", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "companyname",
url: "{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
This will work but I have many controllers and if I add one in the future and forget to adjust the routes it will fail. Also, this is unlikely but if a companyname happens to the be same as one of my controller names it would also fail.
In controller you may redirect to another Controller/action:
public ActionResult yourAction()
{
return RedirectToAction("nameAction","nameController");
}
Did you tried adding this mapping first:
routes.MapRoute( name: "Custom", url: "{id}", defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional } );
That should work but keep in mind that routes are evaluated secuentially, so you will have to organize mappings in order to reach out all pages in your site.
For example, routes like www.example.com/Product could be redirected to /Login by mistake.
EDIT: You can add constraints, so if id is an int value, you can try with the following:
routes.MapRoute("Custom", "{id}",
new { controller = "Login", action = "Index" },
new { id = #"\d+" }
EDIT 2: Having ids as string values, the only solution I see is to manually add each controller as you said, or to add something like this:
routes.MapRoute(
name: "Default",
url: "app/{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
This way you don't need to update each route in the future.
Please try below routing
routes.MapRoute(name: "companylogin", url: "companylogin/{id}", defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
});
routes.MapRoute(name: "default", url: "{controller}/{action}/{id}", defaults: new
{
controller = "Login",
action = "Index",
id = UrlParameter.Optional
});
Remove other controller specific routing. Now you can navigate to login using
url : - www.example.com/companylogin/{id} and all other url redirect default route.
I want users to be able to access the "/Linecard" page of my ASP.Net MVC site using "/Linecard" or "/Manufacturers" as the URL... so same controller, 2 different possible URLs.
I tried adding the following:
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
Adding this after the "Default" route doesn't work at all and I get a 404 error when I go to "/Manufacturers". Putting it BEFORE "Default" works, but then only "/Manufacturers" shows up in the URL when I click menu links since it is the first match. I would like "/Linecard" to always show as the URL.
Any pointers? Is there a certain constraint I can use to accomplish this? Thanks!
I had the same problem when we moved to extension-less URLs. We needed to continue to support one route with extensions. I got around it by having my default route apply to everything except the old URL, then after that mapping one specifically for the exception
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
// if controller specified does not match 'manufacturers' (case insensitive)
new { controller = "^((?i)(?!manufacturers).)*$" },
new string[] { "Namespace.Of.Controllers" }
);
routes.MapRoute(
"Manufacturers", // Route name
"Manufacturers/{action}/{id}", // URL with parameters
new { controller = "Linecard", action = "Index", id = UrlParameter.Optional },
new string[] { "Namespace.Of.Controllers" }
);
You could also set an order when mapping your routes with the defaults at the end like so
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have an web app build with asp.net MVC 4.
I want to have the following 3 types of routes:
/action
/action/id
/id/id2
In global.asax I have changed the routes as it follows:
routes.MapRoute(
name: "Without Action",
url: "{id}/{id2}",
defaults: new { controller = "Home", action = "City_Category" },
namespaces: new[] { "Namespace.Controllers" }
);
routes.MapRoute(
name: "Without Controller",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Namespace.Controllers" }
);
But when I try an {action}/{id} it goes to the first route defined in global.asax. Works only if url is {action} or {id}/{id2}.
How can I make to work all 3 routes?
Thanks!
If {id} and {id2} will always be numeric, you can add a constraint to the route so that it will only kick in when those values are digits:
routes.MapRoute(
name: "Without Action",
url: "{id}/{id2}",
defaults: new { controller = "Home", action = "City_Category" },
new { id = #"\d+", id2 = #"\d+" }
namespaces: new[] { "Namespace.Controllers" }
);
Another idea is to use http://attributerouting.net/, you can define routes by attributes, which is a very easy to define routes, especially if you end up with a lot of complex routes. I had about 30 route definitions and a lot of comments how the url looks like for each action. I could remove all comments and route definitions with these attributes.
I have issue in Routing in Mvc 4
My url goes like this
http://localhost:portnumber/Session/View?Id=918&Pid=186
I want my url to be like this
http://localhost:portnumber/Session/View/918/186
I have view like this
#Html.RouteLink("more..", "Default", new {Controller="Session",Action="View",Id=e.Id,Pid=e.Pid })
routes.MapRoute(
name: "SessionView",
url: "{controller}/{action}/{Id}/{Pid}",
defaults: new { controller = "Session", action = "view", Id = UrlParameter.Optional, Pid = UrlParameter.Optional }
);
The problem is that you're not referring to the correct route.
In the routing table, you've added a route with the name "SessionView", but in your #Html.RouteLink, you refer to a route called "Default".
The correct call should be:
#Html.RouteLink("more..", "SessionView", new {Controller="Session",Action="View",Id=e.Id,Pid=e.Pid })
Just try this
#Html.ActionLink("more..", "View", "Session", new {Id=e.Id,Pid=e.Pid })
Description:
Html.ActionLink(<<LinkText>>,
"<<ActionMethod>>",
"<<Controller Name>>",
new { Id=e.Id,Pid=e.Pid }, // <-- Route arguments.
)
This is my route registration code:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"course_list",
"course/list",
new { controller = "course", action = "list" }
);
routes.MapRoute(
"course_view",
"course/view/{id}",
new { controller = "course", action = "list", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have a link /course/view/87
And the route that is matched is /course/list
Can anyone explain why?
Thank you
UPDATE:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"course_list",
"course/list",
new { controller = "course", action = "list" }
);
routes.MapRoute(
"course_view",
"course/view/{id}",
new { controller = "course", action = "view", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But I'm still getting the same issue.
When i visit: /course/view/87 i get a 404 error.
It appears that your route for course/view/{Id} has a 'list' action. I expect this is a typo.
Adding these routes to an empty Asp.Net Mvc 4 project and using routedebugger (http://nuget.org/packages/routedebugger/), I get a Matched Route of "course/view/{id}". You should use routedebugger locally to see what is going on. The above code seems to be fine.
The button element is treated as submit button (i.e.: type="submit" if not default type attribute is set). Therefore, the browser initiated a post request, which no route satisfied, since all my actions are get(s).
Thank you all for your time.