How can we create a route that will detect that the URL matches a route but that the parameter to that route is wrong?
I've had several cases recently where a user has mistyped a URL and it has been hard to debug because the route table doesn't handle these misspellings very gracefully:
This route handles the URL /Widgets/guid
routes.MapRestfulRoute(
"WidgetsItem",
"Widgets/{idString}",
new { controller = "Widgets", action = "Item" },
new { idString =
#"^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$"}
);
routes.MapRoute("WidgetsList", "Widgets", new { controller = "Widgets", action = "List" });
routes.MapRestfulRoute("CatchAll","{*url}", new {controller = "Error", action = "Http404"});
And the user entered the URL /Widgets/25246810-4b60-4db8-8567-8db8826 which looks correct but is not (there's only 28 characters in that guid provided).
Because the URL didn't match the route, the response was 404. But this is deceptive and caused a debugging problem: it looks like the Widgets/Item action is returning 404. And we spent hours trying to figure out what was wrong with that action, when in fact the action wasn't even called. It wasn't until I installed RouteDebugger that I noticed the guid was malformed.
How can we create another error route that will detect that the URL does actually match a route but that the parameter to that route is wrong?
EDIT: I can "fix" it by adding a new route that matches /Widgets/anything, and placing that route after the normal route:
routes.MapRestfulRoute(
"WidgetsItemError",
"Widgets/{idString}",
new { controller = "Widgets", action = "Item" },
new { idString = #"^.+$" } // match if it has any param at all
);
But this seems to be very inefficient - I will have to add a similar error route for every single route eg /Foo, /Bar /FooBar etc (and I've got quite a lot of routes). Isn't there a more generic way to do this?
When I dealt with guid's in my routes, I would accept a string, try to parse the string value to a guid, if it didn't parse, i'd return a different view with a message indicating that the id was invalid and to try again
The only thing you should have to change here would be to remove the route constraint and change your Action to accept a string instead of a guid (unless your action is already accepting a string)
The easiest solution is to remove the route constraint, then check for a null parameter in your controller and do whatever you want from that point.
Related
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToRoute("Home");
}
I expect this action to redirect the user to the homepage but instead, a redirect loop occurs (according to Chrome).
The aforementioned action belongs to a controller in the "admin" area as where the "Home" route is defined for the default area - I suspect this to be relevant.
Here is the route for good measure:
routes.MapRoute(
"Home",
"{controller}/{action}/{slug}",
new { controller = "Posts", action = "Index", slug = UrlParameter.Optional },
new[] { "GoBlog.Controllers" }
);
Update
Replacing the return statement with the following will cause the action to work as expected:
return RedirectToRoute("Home", (RouteTable.Routes["Home"] as Route).Defaults);
I want to know why this is the case. Ideally I can omit the (cumbersome) second argument.
This issue is particular to the call RedirectToRoute(string) and your particular route:
routes.MapRoute(
"Home",
"{controller}/{action}/{slug}",
new { controller = "Posts", action = "Index", slug = UrlParameter.Optional },
new[] { "GoBlog.Controllers" }
);
What you expected was RedirectToRoute would populate your arbitrary route definition of {controller}/{action}/ with the route's defined defaults:
controller = "Posts", action = "Index"
But RedirectToRoute like all redirect methods, is a part of your controller, and uses your current Controller and Action as Default Values where ever possible. This is a "feature" of the ASP.NET MVC framework -- re-using routes to create new routes. This approach is valuable because your current Controller and Action are strong hints as to your intention.
A great example of this is RedirectToAction(string) which assumes your current instantiated Controller is the default.
Contrast this with:
return RedirectToRoute("Home", (RouteTable.Routes["Home"] as Route).Defaults);
Rather than directing the framework to use it's best guess about how to populate the ambiguous wild card route, you have specifically instructed the framework to use the Defaults from the RouteTable for "Home" route to construct a route and return it as a RouteObject.
When you call RedirectToRoute(string)
This method converts the route name that is passed in routeName to a URL by using the RouteCollection.GetVirtualPath method.
where pass null as parameter RouteValueDictionary. So in this case this parameters was getting from current RequestContext with values for controller, action and etc, i.e. you get url like this
"{controller}/{action}/{slug}"
"Login/Logout/"
and got redirecting loop.
when you call
return RedirectToRoute("Home", (RouteTable.Routes["Home"] as Route).Defaults);
instead RedirectToRoute(string) used RedirectToRoute(String, RouteValueDictionary) and in RouteCollection.GetVirtualPath method you pass defaults values for that route.
It is a general best practice in MVC to allow your controllers as much control as possible. There are multiple controller methods which you can use, and you should generally use the one that is the closest fit to the process you are performing.
return View(). tells the controller to generate and return the specified HTML, similar to a Server.Transfer(). Use for matching HTML with Actions, or when delivering static HTML without interest in the URL string.
Return RedirectToAction(). Tells the controller to build a new URL string based on the Action. This will parse the route dictionary for the Action, and handle the defaults. It acts as a Response.Redirect(), and will return a new generated URL. In general, this is the most commonly used redirect. This also has the benefit of not changing if the route dictionary is changed.
return RedirectToRoute(). Similar to RedirectToAction(), however tied to the Route Dictionary. This will search the Route Dictionary for an exact route match, using whatever route parameters are passed. This does not look for Actions, and thus does not build a route or inject default values. Used when an exact route with specific non-default values must be processed.
return Redirect(). This redirect requires a full URL, and will redirect to the new URL. Usually used when redirecting to a URL on a totally different domain.
Basically I would like to take an url like:
http://example.com/community/www.foo.com/bar
...and give it to my http://example.com/community/ view with www.foo.com/bar in some variable that razor can access in the view.
How could this be done?
Note: I tried this:
routes.MapRoute(
"Community",
"{controller}/{url}",
new { controller = "Community", action = "Index", url = "" }
);
which worked with http://example.com/community/www.foo.com but not http://example.com/community/www.foo.com/bar (IIS tried to resolve the latter and gave a 404).
From MSDN:
Sometimes you have to handle URL requests that contain a variable
number of URL segments. When you define a route, you can specify that
if a URL has more segments than there are in the pattern, the extra
segments are considered to be part of the last segment. To handle
additional segments in this manner you mark the last parameter with an
asterisk (*). This is referred to as a catch-all parameter. A route
with a catch-all parameter will also match URLs that do not contain
any values for the last parameter. The following example shows a route
pattern that matches an unknown number of segments.
query/{queryname}/{*queryvalues}
In your case it will be:
routes.MapRoute(
"Community",
"{controller}/{*url}",
new { controller = "Community", action = "Index", url = "" }
);
Alternative and more robust approach is using some kind of encoding for the URL part - for example Base64URI encoding from RFC4648
I have some problem with routing in my ASP.NET MVC3 application, exactly it doesn't works as I want.
Routing definition in Global.asax
routes.MapRoute(
"News", // Route name
"{lang}/{controller}/{action}/{page}/{id}", // URL with parameters
new { lang = "pl", controller = "News", action = "Index", page = 1, id = UrlParameter.Optional } // Parameter defaults
);
So I want use it in 2 situation:
/{lang}/{controller}/{action}/{page} - to go to specific paginations' page
/{lang}/{controller}/{action}/{page}/{id} - to remember a current page, and go to page with specific id.
to generate links I use:
#Html.RouteLink(i.ToString(), "News", new { lang = ViewBag.Lang, page = i })
where i is number of page, and lang is language.
In Controller i handle it witch Action:
public ViewResult Index(string lang, int page = 1)
PROBLEM
#Html.RouteLink gives me link /pl/News/Index/2 (when '2' was clicked) but my controller doesn't handle it and page is still '1'. So when I click the same link twice it gives : /pl/News/Index/2/2 and then page equals '2'.
If somebody can explain me how it exactly works, and what can I do to solve it?
Any help would be appreciated.
I guess what you're missing here is that the routing system uses the variable values from the current request to generate the outbound rules.
For example - if you're on the page /pl/News/Index/2 you have
lang = "pl", controller = "News", action = "Index", page = 2
if you try to generate a rule like this
#Html.RouteLink(i.ToString(), "News")
the routing system will use the already present variables like lang and page and will generate exactly the same URL - /pl/News/Index/2.
Of course values that you pass in the anonymous type in Html.RouteLink are treated with priority and will override values from the current request. So in your example you should consider what URL are you at when you generate the link.
Order of precedence is:
variables passed in the anonymous type
request variables
default values defined in the route.
It seems that this part
lang = ViewBag.Lang
is useless since lang will be taken from the request if you don't supply it - so it will be preserved.
Update(might not be relevant):
You should also keep in mind that if you have more than one match for a rule - the routing system will choose the first one which satisfies the constraints(for example values from request should not conflicts with default variables)
Update2:
The routing system uses request variables up to the first explicitly specified variable(via the anonymous type) so if you specify lang explicitly - no request variables will be reused(in your case, because lang is the first segment in your url)
I think part of your problem is that you're breaking convention a bit, and since MVC relies on convention to route successfully, your mapping is a bit busted.
I would suggest rewriting your routing like so:
routes.MapRoute(
"News", // Route name
"{controller}/{action}/{lang}/{id}/{page}", // URL with parameters
new { controller = "News", action = "Index", lang = "pl", page = 1, id = UrlParameter.Optional } // Parameter defaults
);
It seems like the most common case scenario for a URL would be something like:
http://yourdomainhere/news/pl/some-sort-of-permalink/1
I'm trying to specify a route configuration that will allow a url similar to:
/{any sequence of characters}/{any sequence of characters}/{mexid}
We're currently redesigning an old system that used URLs of the previous format. Our new system has to catch these requests and redirect them to a new URL, but only if there are 3 URL parameters. It's only the third parameter that I'm interested in.
This is the route mapping that I'm using:
routes.MapRoute(
null,
"{param1}/{param2}/{mexid}",
new { controller = "ShareClass", action = "FundFactsheet", mexid = "" }
);
the problem I'm having is that this is acting as a kind of catch-all for any invalid requests that come in, where I am only interested in this being the route for URLs with a mexid in them.
how can I define a route that will only apply to URLs that contain 3 parameters, only if the URL doesn't already match another route that I've more specifically defined?
You might want to add route constraint (I haven't tested it yet but I think it should answer your question)
routes.MapRoute(
null,
"{param1}/{param2}/{mexid}",
new { controller = "ShareClass", action = "FundFactsheet" },
new { param1 = #".+", param2 = #".+", mexid = #".+" }
);
Can you remove the default for mexid?
The way you have the route right now, if you don't specify mexid then it will use "" as a default.
If you don't have anything unique about param1 and param2 then it will probably always take precedence over the default route, though.
I have a controller named Movie, with an action named ByYear, which takes the year as a parameter :
public ActionResult ByYear(int year)
{
ViewData["Title"] = string.Format("Movies released in {0}", year);
var repository = MvcApplication.GetRepository();
var movies = repository.Medias
.OfType<Movie>()
.Where(m => m.Year == year);
return View("Index", movies);
}
I'd like to access this action with the following URL : /Movie/ByYear/{year}, but the only valid route for this action is this : /Movie/ByYear?year={year}.
I tried to add new routes in my application's RegisterRoutes method, but I can't find a way to get the desired result...
Could anyone tell me how to achieve that ?
Note: this is actually very similar to this question, but no answer was accepted, and the highest voted answer makes no sense to me as I'm completely new to MVC...
Change the name of your parameter year to id and this will match the default route that MVC adds to your project.
So for further clarification, let's take a look at the default route added by ASP.NET MVC:
routes.MapRoute(
"default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
In this route you can see three tokens that are named specifically for controller, action, and the third token which is passed to the action is id. When a request comes into your application, ASP.NET MVC will analyze the routes that are currently mapped and try to find a method signature that matches them by using reflection against your controllers.
When it looks at your Movie controller, it sees an action called ByYear, however that method takes an integer called year, not id. This is why you end up with something like /Movie/ByYear?year={year} when you create an ActionLink for that particular Action. So to fix this, you have two options:
The first and most simple method to fix this is to just change the method signature for your Action to accept a parameter named id which is what I recommended above. This will work fine, but I can see where it might cause a little bit of confusion when you go back to that source later and wonder why you called that parameter id.
The second method is to add another route that matches that method signature. To do this, you should open your Global.asax and just add the following (untested, but should work):
routes.MapRoute(
"MoviesByYear",
"Movies/ByYear/{year}",
new { controller = "Movie", action = "ByYear" }
);
This route is hard-coded, yes, but it won't break the other routes in your system, and it will allow you to call the method parameter year.
EDIT 2: Another thing to note is that the routing engine will stop on the first route it finds that matches your request, so any custom routes like this should be added before the default route so you are sure they will be found.
OK, I just found out how to do it. I just had to create the new route before the default route... I didn't think the order had any significance
routes.MapRoute(
"MovieByYear", // Route name
"Movie/ByYear/{year}", // URL with parameters
new { controller = "Movie", action = "ByYear" } // Parameter defaults
);
EDIT: Isn't there a simpler way ? (not involving renaming the parameters). I'd like to be able to do something like that :
[Route("/Movie/ByYear/{year}")]
public ActionResult ByYear(int year)
{
...
Design considerations aside, if you did not want to rename the parameter, you could add something like the route below, which enforces having the year parameter
routes.MapRoute(
"MovieByYear", // Route name
"Movie/ByYear/{year}", // URL with parameters
new { controller = "Movie", action = "ByYear" },
new { year = #"\d+" } // Parameter defaults
);