I want to add optional parameters in my routing table.
For example I would like the users to browse a product catalog like this:
http://www.domain.com/browse/by-category/electronics/1,2,3 etc
Now i've created a route like this:
routes.MapPageRoute(
"ProductsBrowse",
"browse/{BrowseBy}/{Category}",
"~/Pages/Products/Browse.aspx"
);
Problem however is that when a user enters http://www.domain.com/browse , I would like them to present a different page where they can pick the manner on how to browse. So the parameters {BrowseBy} and {Category} will not be used.
Is there a way around this then to create seperate routes for each of the scenarios?
Thank you for your time!
Kind regards,
Mark
I just came across this question, and knew there had to be way to do this. There is-
MapPageRoute has an overload that will allow you to specify defaults. here's an example usage based on your code:
routes.MapPageRoute(
"ProductsBrowse",
"browse/{BrowseBy}/{Category}",
"~/Pages/Products/Browse.aspx",
false,
new RouteValueDictionary { { "Category", string.Empty } }
);
So if the user doesn't specify a category this route will still be hit. The problem I have with using two separate routes is that I have links setup around my site that are generated by route name, and you cannot have two routes that have the same name.
Here's good documentation from MSDN: here
try this:
routes.MapPageRoute(
"ProductsBrowse",
"browse/{BrowseBy}/{Category}/{*queryvalues}",
"~/Pages/Products/Browse.aspx"
);
I'd just create the separate route.
That said, you could define a custom RouteHandler that based on some convention you define, automatically send those special cases as if you had a different route.
Alternatively you could use the custom RouteHandler along with a convention, to avoid having to specify the specific page in your routes. That's the equivalent of what asp.net MVC does.
Related
it seem that we need to specify a route for every page in webform routing
I want to use default route to a page called cms.aspx with parametr called nameofurl for each page expect default.aspx
sometimes I want to send the cms.aspx more then one parametrs,for example
mydomain.com/cms.apx?nameurl=somevalue
or
mydomain.com/cms.apx?nameurl=somevalue&order=6
I have this code but it isn't the solution since you have to tell the routing the name of the page
routes.MapPageRoute("",
"pageName/{nameofurl}",
"~/cms.aspx")
I want something like this
routes.MapPageRoute("",
"?/{nameofurl }",
"~/cms.aspx")
sometimes I want it to be like this
routes.MapPageRoute("SalesRoute",
"?/{nameofurl}/{order}",
"~/cms.aspx");
any idea how to Achieve that kind of routing without specify the name of the page?
You can create routes like given below:
routes.MapPageRoute("Route1","{nameofurl}","~/cms.aspx")
routes.MapPageRoute("Route2","{nameofurl}/{order}","~/cms.aspx")
routes.MapPageRoute("Route3","{nameofurl}/{order}/{abc}","~/cms.aspx")
The above routes will work if there are no other pages with 2 or 3 parameters. But if there is some other page which you want to route and which has 2 parameters to be passed, then you need to mention a hard-coded string before the parameters otherwise the new route will override the old route.
For Example:
routes.MapPageRoute("Route4","{nameofurl}/{order}","~/products.aspx")
In the above case, Route4 will override Route 2. Thus, you need to define route something like below:
routes.MapPageRoute("Route4","products/{nameofurl}/{order}","~/products.aspx")
You can find URL Routing related articles at following URLs:
http://karmic-development.blogspot.in/2013/10/url-routing-in-aspnet-web-forms-part-2.html
Thanks & Regards,
Munjal
I want my url to look like this www.website.com/browse/computers/consoles/playstation-3/en-NZ.aspx. The part where it starts - browse which I guess is my controller - computers/consoles/playstation-3 is my action and - en-NZ.aspx is the page I want to name it. My three questions are how do you set up the action part when there are multiple departments and what is the regular expression for en-NZ for the CultureInfo is it /\{2, [a-z]}\-\{2, [A-Z]} I also need that expression to have it for two lower case letters for turkey e.g tr wich is the only language code with two letters. And how do you access a particluar department in the url after the {browse}/{*multipleDepartments}/ part.
void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"browse", //Name of the Route (can be anyuthting)
"{*departments}/regexp goes here.aspx", // parameters not to sure about the*
"~/Departments.aspx"); //Page which will handles and process the request
}
check my comments
Also I have created a CTE Expression which createds the Url for me it builds a link like the example above /computers/consoles/playstation-3 if I have to put that in parameters how Do i deal with that in a parameters url, what I mean is the parameters would be multiple on the same query like Departments.aspx?a=computersb=consoles&c=playstation-3. I wouldn't know how deep the query is so what do I do if it's in this case dealing with parameter.
I think you are going about this the wrong way... the culture info shouldn't be the page name, it should be part of the url. You don't normally create separate pages for different languages, instead you use resources to localise a page.
With URL routing, generally the make-up of a route follows the pattern {controller}/{action}/{id}.
So the URL you are looking to route would look similar to this:
www.website.com/en-NZ/consoles/list/playstation-3
For further reference, check out these good tutorials:
Scott Guthrie: ASP.NET MVC Framework (Part 2): URL Routing
Stephen Walther: ASP.NET MVC Routing Overview
Im adding the filevistacontrol to my asp.net MVC web application.
I have a media.aspx page that is ignored in the routing with
routes.IgnoreRoute("media.aspx");
This works successfully and serves a standard webforms page.
Upon adding the filevistacontrol, I can't seem to ignore any calls the control makes to it's webservice.
Eg the following ignoreRoute still seems to get picked up by the MvcHandler.
routes.IgnoreRoute("FileVistaControl/filevista.asmx/GetLanguageFile/");
The exception thrown is:
'The RouteData must contain an item named 'controller' with a non-empty string value'
Thanks in advance.
Short answer:
routes.IgnoreRoute( "{*url}", new { url = #".*\.asmx(/.*)?" } );
Long answer:
If your service can be in any level of a path, none of these options will work for all possible .asmx services:
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
By default, the parameters in a route pattern will match until they find a slash.
If the parameter starts with a star *, like pathInfo in those answers, it will match everything, including slashes.
So:
the first answer will only work for .asmx services in the root path, becasuse {resource} will not match slashes. (Would work for something like http://example.com/weather.asmx/forecast)
the second one will only work for .asmx services which are one level away from the root.{directory} will match the first segment of the path, and {resource} the name of the service. (Would work for something like http://example.com/services/weather.asmx/forecast)
None would work for http://example.com/services/weather/weather.asmx/forecast)
The solution is using another overload of the IgnoreRoute method which allows to specify constraints. Using this solution you can use a simple pattern which matches all the url, like this: {*url}. Then you only have to set a constraint which checks that this url refers to a .asmx service. This constraint can be expressed with a regex like this: .*\.asmx(/.*)?. This regex matches any string which ends with .asmx optionally followed by an slash and any number of characters after it.
So, the final answer is this:
routes.IgnoreRoute( "{*url}", new { url = #".*\.asmx(/.*)?" } );
I got it to work using this (a combo of other answers):
routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
What happens when you use:
routes.IgnoreRoute("FileVistaControl/filevista.asmx");
If that doesn't work, try using the ASP.NET Routing Debugger to help you:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Try this:
routes.IgnoreRoute("{*filevista}", new { filevista = #"(.*/)?filevista.asmx(/.*)?" });
This is based on a Phil Haack recommendation stated here.
Have you tried:
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
It would help if you posted the source for your route configuration. I'm going to take a shot in the dark and say to make sure that your IgnoreRoute() calls are all at the top of your routing definition.
The way IgnoreRoute works is to create a route that matches the ignored route URL and constraints, and attaches a StopRoutingHandler as the RouteHandler. The UrlRoutingModule knows that a StopRoutingHandler means it shouldn't route the request.
As we know, the routes are matched in the order of which they are defined. So, if your {controller}/{action}/{id} route appears before your "FileVistaControl/filevista.asmx/GetLanguageFile/" route, then it will match the "{controller}/{action}/{id}" route.
I may be totally off base here, but it's hard to know without seeing your source. Hope this helps. And post source code! You'll get better answers.
i want to make some questions about asp.net mvc.Actually,i am not familiar with web developing.my questions are i have two controllers,one is login and another one is
profile.After login,i want to call profile.so,i use this way,
return RedirectToAction("DiaplayProfile","Profile",new { personID = Person.personID});
my problem is parameter that i passed are shown i URL.i don't want to show.In web developing,if we use post method to send data,parameters are not shown in url,is it correct?for me,i need to pass parameter ,but,i don't want to show at url.
How i use in post method in MVC RedirectToAction?i also tested with TempData
to pass data,but,it's gone after if i make refresh.how i hanlde this?And also,
can i use [AcceptVerbs(HttpVerbs.Post)] for solve this problem?
guide me right ways, please.
Regards
Chong
You can't do a post as part of a redirect; a redirect must be a get request since you're simply passing the browser the URL to which to redirect. In MVC, having the id as part of the URL is actually considered to be a good thing. I would embrace it. Rather than having the parameter be personID, I would use id so that the default routing can be used. Eventually I'd want to end up with a URL that looks like:
http://example.com/profile/display/1345
where 1345 is the id of the person in question. If you use default routing, that would mean that you have a ProfileController with a Display method that takes a single id parameter (of type int). You would redirect to it using:
return RedirectToAction( "display", "profile", new { id = 1345 } );
BTW, you can't use the AcceptVerbs attribute to control how a redirect is made, only to control what HTTP verbs the action will respond to. In this case, I think you really do want to accept a GET and I think you really do want to have the id in the URL.
I'm using asp.net routing in a webforms app.
I would like to achieve the following url format:
http://[domain]/{parent-category}/{sub-category}/{sub-category}
where the right most category is available as a route value.
Currently I have achieved this with the following route:
routes.MapPageRoute(
"category-browse",
"{*category}",
"~/category.aspx"
);
This will pass all of the categories i.e. "trainers/running/nike-running-trainers" so I can grab the last one with a bit of string manipulation.
Is there a better way of doing this?
I assume you can have an arbitrary number of sub-category parameters. If that's the case, the approach you're doing is the right one. ASP.NET Routing doesn't support having a catch-all parameter in the middle of the URL. It must be at the end. So what you described is the only way to do it short of writing your own custom RouteBase implementation.