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
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 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
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
The reason I'm asking is because IIS protects certain ASP.NET folders, like Bin, App_Data, App_Code, etc. Even if the URL does not map to an actual file system folder IIS rejects a URL with a path segment equal to one of the mentioned names.
This means I cannot have a route like this:
{controller}/{action}/{id}
... where id can be any string e.g.
Catalog/Product/Bin
So, instead of disabling this security measure I'm willing to change the route, using a suffix before the id, like these:
{controller}/{action}_{id} // e.g. Catalog/Product_Bin
{controller}/{action}/_{id} // e.g. Catalog/Product/_Bin
But these routes won't work if the id contains the new delimeter, _ in this case, e.g.
// These URL won't work (I get 404 response)
Catalog/Product_Bin_
Catalog/Product/_Bin_
Catalog/Product/__Bin
Why? I don't know, looks like a bug to me. How can I make these routes work, where id can be any string?
Ok, I have a definitive answer. Yes, this is a bug. However, at this point I regret to say we have no plans to fix it for a couple of reasons:
It's a breaking change and could be a very hard to notice one at that.
There's an easy workaround.
What you can do is change the URL to not have the underscore:
{controller}/{action}/_{id}
Then add a route constraint that requires that the ID parameter starts with an underscore character.
Then within your action method you trim off the underscore prefix from the id parameter. You could even write an action filter to do this for you if you liked. Sorry for the inconvenience.
You can use characters that are not allowed for a directory or file name like: *,?,:,",<,>,|.
With ASP.NET MVC if you look at the source they have a hard-coded value for the path separator (/) and to my knowledge cannot be changed.
I'm reading about URL routing at How to: Define Routes for Web Forms Applications and there's something in the example I don't understand. If you look at the example provided below,
routes.MapPageRoute("", "SalesReport/{locale}/{year}/{*queryvalues}", "~/sales.aspx");
specifically at
"SalesReport/{locale}/{year}/{*queryvalues}"
Why does queryvalues have an asterisk in front of it and locale and year don't?
The * indicates a "catch all" parameter, which essentially matches everything else in the requested URL.
Everything after the "year" parameter in the URL will get dumped into the queryvalues parameter. So for example, the URL
http://whatever/SalesReport/canada/1999/x=1
will give you a queryvalues variable populated with "x=1". But it will also match the URL
http://whatever/SalesReport/canada/1999/x=1/y=2/z=3
and queryvalues will be populated with "x=1/y=2/z=3".
You can only have one catch-all parameter in your route, and it has to be the final parameter.