default URL Routing with ASP.NET 4 Web Forms - asp.net

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

Related

ASP.NET URL Routing with wildcard

I have a CMS system that I am using Routing to get the page name. I have the need to have unlimited values (sub directories, product names, different localizations) between the first item and the last item (page name).
For example:
/Products/Computers/ComputerType1/
And
/Productos/Ordenadores/ComputerType1/
Where ComputerType1 is the page name.
routes.Add(new Route("{*route}/{pageName}", routeHandler));
I cannot find a way to make the middle part ({*route}) of the route to be the wildcard so that unlimited number of sub directories can be put in front of the page name. Currently I have only been able to get around this with having a default wildcard route such as:
routes.Add(new Route("{*route}", routeHandler));
to catch everything. However, the wildcard seems to also be letting in gif urls even thou I have it specified as ignore above in the route code as:
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.Ignore("{resource}.asmx/{*pathInfo}");
routes.Ignore("{resource}.ashx/{*pathInfo}");
routes.Ignore("{resource}.gif/{*pathInfo}");
routes.Ignore("{resource}.png/{*pathInfo}");
routes.Ignore("{resource}.jpg/{*pathInfo}");
routes.Ignore("{resource}.ico/{*pathInfo}");
routes.Ignore("{resource}.pdf/{*pathInfo}");
routes.Ignore("{resource}.css/{*pathInfo}");
routes.Ignore("{resource}.js/{*pathInfo}");
Is there a better way of doing this? Should this be handled thru a custom route handler?
yes you should create a route handler for cases like these
simple create a class and derive it from RouteBase
override the GetRouteData method
in this method you can access the current httpcontext and thus you can access the requested URL
so u can route accordingly.
for more info on custom routes visit this link
http://www.codeproject.com/Articles/299531/Custom-routes-for-MVC-Application

Url Routing Question multiple departments

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

ASP.NET webforms wildcard route

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.

asp.net webforms routing: optional parameters

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.

MVC Routes: Adding an optional parameter at the beginning of a route

I have the following routes setup in my route config file. I have a config reader that maps these to MVC-style routes.
[route name="customers" url="customers/{statename}/{marketname}/{pagenumber}"]
[controller name="Customers" action="Display" /]
[/route]
[route name="pcustomers" url="{customername}/customers/{statename}/{marketname}/{pagenumber}"]
[controller name="Customers" action="Display" /]
[/route]
As you can tell, the first and second route are pretty much the same but for the {customername} part in the second one.
The first one matches urls like
www.abc.com/customers/TX/Austin/5
where as the second one matches urls like
www.abc.com/CustomerX/customers/TX/Austin/5
My question is, is there a way to combine these two routes into one and still be able to match both the urls?
Could you use subdomains and change the second URL to customerx.abc.com/customers/tx/Austin/5? What about tacking the customerx potion onto the end as an optional parameter like so?
abc.com/customers/tx/Austin/5?customer=x
I would've made the route like this:
customers/{statename}/{marketname}/{customer}
and do the pagenumber as a querystring.
That way the url would be:
www.abc.com/customers/tx/Austin?pagenumber=1
or
www.abc.com/customers/tx/Austin/CustomerX
The construction of the url will also most likely follow the usagepattern of the site aswell:
Click Customers
Select a State
Select a marketname
Browse pages
Click the customer

Resources