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
Related
I am using asp.net mvc to build a page where i have to show alot of users in a table. I would like to make a paged view with an url like:
mypage.com/Customer/{pageno}/{sorttype}/{somethingelse}
The urls would be located different places on a page. E.g. the "next page" button would have the same url, with just {pageno} increased, and the table headers would have {sorttype} different.
Is there a nice way to do this in razor, or will i just have to get the raw url, and parse it myself?
Thanks.
You should leave URL generation to ASP.NET MVC routing. In the global.asax you should configure route for your Customer page correctly so it can take pageno, sorttype and somethingelse parameters.
Then in Razor you should simply use Url.Action (or Html.ActionLink) method, so the URL would be generated for you. You can use following overload in your case:
http://msdn.microsoft.com/en-us/library/dd470197(v=vs.118).aspx
If you find it too complex to write stuff like #Url.Action("Index", "Customer", new { pageno = 1, sorttype = "type", somethingelse = "test"), then you can add another extension method like Url.MyAction which will take 3 parameters (int pageno, string sorttype, string somethingelse) and call Url.Action internally. However I wouldn't suggest doing that because the readability of your code will surprisingly decrease. Everybody know Url.Action method, while your new Url.MyAction would be something new to other developers reading your code.
Again, the key is correct routing, so Url.Action() will be rendered to "/Customer/1/type/test". You can find more information on the link below: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs
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'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.
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.