I have tried routes.mapRoute but i can't figure a way in MVC 3 to use it make a root path route to an action. e.g mywebsite.com/party should redirect to mywebsite.com/events/party where events is the controller and party is the action.
Is this even possible?
Without seeing your existing routes, its hard to give you an exact solution.
One rule to keep in mind:
MVC will resolve the first in your route collection that matches the requested URL
not necessarily the most specific match.
Make sure you do not have another rule that would also satisfy that route placed earlier in your code, e.g. the routing algorithm might be finding a "party" controller and "index" action because you have a default rule like:
routes.MapRoute(
"Default",
"{action}",
new { controller = "Home", action = "Index" }
);
placed before your rule.
You need to put something like
routes.MapRoute(
"PartyRoute",
"party",
new { controller = "Events", action = "Party" }
);
BEFORE any route that might match a URL with just a single parameter
routes.MapRoute(
"Default",
"{action}",
new { controller = "Events", action = "Index" }
);
Related
In MVC, the default route url pattern is - url : "{controller}/{action}/{id}"
When I add a new route as shown below before the default route, the url for the default route is shown as something like Home/Index?id=5 and not Home/Index/5. How can this be fixed.
routes.MapRoute(
name: "Name",
url: "{controller}/{action}/{name}",
defaults: new { controller = "Home", action = "Browse", name = UrlParameter.Optional }
);
The default route will never be hit because the route you added is exactly the same, from a routing perspective. So your route will catch everything that the default route would catch if it were the only one, or placed before yours. Both will match one, two and three-segment URLs.
That route is unnecessary and pretty much useless.
like what #asymptoticFault says, it serves the same purpose as the default one.
I have default routing set for my mvc application like:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have Home controller with Index() and About(). Tell me please how to modify routing to get both domain.com/Index and domain.com/About urls?
Thank you
Add this before your default route. By adding it before the default route, if it matches it will be used to set the RouteDictionary values. Untested, but it should map urls that only have a single component that is either index or about. Note, that this assumes you don't have an index or about controller. The routing constraint is important as it keeps it from matching on each controller's index action, e.g., controller/.
routes.MapRoute(
"IndexOrAbout",
"{action}",
new { controller = "home", action = "index", id = "" },
new
{
action = "(index)|(about)"
}
);
Note, if you need to expand this to more top-level routes or make it more dynamic you could use a custom routing constraint that could draw the top-level values from a database or configuration. At that point, you'd probably want to change it from using the action parameter to the id parameter and have a single action that use the id to determine what to show rather than have an action per value.
Here are the URL's that I'm trying to map with MVC3
routes.MapRoute( "Products", "{controller}/{id}/{*name}", new { action = "view" }, new { id = #"\d+" } );
/products/13/seo-friendly-name-of-the-product
Now the next route I need to map is this
routes.MapRoute( "General", "{controller}/{id}/{action}", new { }, new { id = #"\d+" } );
/user/42/changepassword
I want to know how to resolve this problem. Simply changing the order isn't enough because one area of the app stops working. I know that {*name} and {action} are being conflicted, but I don't know what to do to fix this.
The goal is to eliminate ambiguous matches, so let's look at making one more specific. Is the controller for your Products route always "Products"? If so, could you change that route to
routes.MapRoute(
"Products", // Route name
"products/{id}/{*name}", // URL with parameters
new { action = "view", controller="Products" }, // defaults
new { id = #"\d+" } // constraints
);
This conflict is difficult to resolve simply because:
/products/13/seo-friendly-name-of-the-product
/user/42/changepassword
are the same and it will always be the first route that will pick the request. You might need to put some constraints (like for example saying that the catch-all value will always contain dashes which would disambiguate it from the other route because an action name cannot contain dashes or fix the controller for the catch-all route).
I have this two routes.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"Paging", // Route name
"{controller}/{action}/p{currentPage}",
new { controller = "Home", action = "Index" },
new { currentPage = "\\d+" });
I have this Controller
public class MyController
{
public ActionResult All(int currentPage = 1)
{
// some code executed here
return View(pList);
}
}
Why this url goes to the first route /My/All/p5
Can someone point me to good tutorial about routes?
Routes need to be registered in the right order, as they are processed in the same order in which they are registered. Your first route is essentially a catch all, so it will also match /My/All/p5. Register that route first:
routes.MapRoute(
"Paging", // Route name
"{controller}/{action}/p{currentPage}",
new { controller = "Home", action = "Index" },
new { currentPage = "\\d+" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Reading : Steven Sanderson has very good explanation of routing in his book ( Pro MVC 2 ) in chapter 8. (You can find it here)
From the book:
If there’s one golden rule of routing,
this is it: put more-specific route
entries before less-specific ones.
Yes, RouteCollection is an ordered
list, and the order in which you add
route entries is critical to the
routematching process.
I have a series of blog posts on Routing you can read here: http://haacked.com/tags/Routing/default.aspx
Also, the route debugger http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx is a useful tool for playing around with routing and understanding why a route you think should match is not matching.
BTW, Matthew Abbot is correct. You need to re-order the routes. Nazar's quote from Steven Sanderson's book has the reason why that's the case. Routing evaluates routes in order and the first one wins.
Here's the simple exercise I would have done to debug this situation. Looking at your request:
/My/All/p5
I would go through each route one at a time in my system and ask, "would it match?". The first route where the answer is yes is the one that will match. In your example, you can see that route is the first route. This is why Steven suggests putting more-specific routes first, so they match.
And the Route Debugger I mentioned earlier does this exercise for you. It shows you every route that would match a given request.
I'm trying out ASP.NET MVC routing and have of course stumbled across a problem. I have a section, /Admin/Pages/, and this is also accessible through /Pages/, which it shouldn't. What could I be missing?
The routing code in global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Pages", // Route name
"Admin/Pages/{action}/{id}", // URL with parameters
// Parameter defaults
new { controller = "Pages", action = "Index", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
// Parameter defaults
new { controller = "Home", action = "Index", id = "" }
);
}
Thanks!
I'd suggest adding an explicit route for /Pages/ at the beginning.
The problem is that it's being handled by the Default route and deriving:
controller = "Pages"
action = "Index"
id = ""
which are exactly the same as the parameters for your Admin route.
For routing issues like this, you should try out my Route Debugger assembly (use only in testing). It can help figure out these types of issues.
P.S. If you're trying to secure the Pages controller, make sure to use the [Authorize] attribute. Don't just rely on URL authorization.
You could add a constraint to the default rule so that the {Controller} tag cannot be "Pages".
You have in you first route {action} token/parameter which gets in conflict with setting of default action. Try changing parameter name in your route, or remove default action name.