MVCSiteMapProvider DymanicNode RouteValues Confusion - asp.net

I am facing some problem or maybe I am confused. I've followed the following link to generate dynamic Site Map:
MVCSiteMapProvider Dynamic Sitemap
I am confused at node.RouteValues.Add("id", album.AlbumId); in the class given in above link. My website links are not in the form given in the example they discussed (their URLs are like mysite.com/controller?id=some id)
where as my URLs are in following format:
mysite.com/mycontroller/querystring1/querystring2/querystring3
How can I "mention" such kind of URL in node.RouteValues.Add(..., ...); so that it should redirect to mysite.com/controller/querystring1/querystring2/querystring3?
Thanks

The format of the URLs makes no difference. Either way, they are transformed into a collection of route values, which is what MvcSiteMapProvider uses under the hood.
Therefore, the same line node.RouteValues.Add("id", album.AlbumId); can be used in either scenario to make the node match. The actual processing of the URL into route values is done by .NET routing, not MvcSiteMapProvider.

Related

Does Kentico allow query strings with question mark?

I'm trying to migrate my ASPX site to Kentico, and as part of my task I'm migrating URLs. I need to preserve my URL structure, so I need to keep URLs which look like : "foo.com/bar.aspx?pageid=1".
I checked page's "URLs" property tried to use wildcards, some patterns like /bar/{pageid}- /bar/{?pageid?}-, etc but Kentico always replaces question marks.
Is there a way to achieve that via the admin interface?
You don't need to do anything in order to use "foo.com/bar.aspx?pageid=1" url.
Create a page under the root and call it bar, so you'll get a page # foo.com/bar.aspx. Kentico and/or .net does not care what you add to a url after question mark, so foo.com/bar.aspx?pageid=1 will work as well as foo.com/bar.aspx?someparam=sdf, or foo.com/bar.aspx?id=1&p=3&t=3.
You may (or may not) implement some functionality based on query string (e.g. paging), so it will parse query string and act in appropriate way.
By default Kentico UI does not handle adding URL aliases with URL parameters like you show. There is an article on the DevNet for a URL Redirection module which has code you can import into your site to allow you to perform these redirects within the Kentico UI. I'd suggest using this approach.
Unfortunately, I can't share a code sample since it's an article but it also has a link to download the code too. This appears to only be coded for Kentico 8.2 right now but I'm guessing you could do some work to make it work for other versions if you needed.
I think there are few concepts that you are clubbing here. I will start with your line code here
/bar/{pageid} - {pageid} is a positional parameter in Kentico's language if you choose to use dynamic URLS based on patterns. SO if you have a code that relies on pageid parameter to fetch some data then Kentico will pass that value. E.g in case of /bar/420, it will pass pageid as 420 different web parts on your template
/bar/{?pageid?} - This will search for query string parameter "pageid" on the request URL and replace its value here. So if you passed foo.com/bar.aspx?pageid=366, the resulting URL will be /bar/366
The #1 is positional parameter and #2 is the way in which Kentico resolves query string macros.
I hope this clarifies.

Symfony2 GenerateURL to a complex route

This seems like the dumbest question ever, but I can't seem to figure it out. I have a page the needs to redirect to a complex route and I can't seem to generate the URL. Redirecting to a simple route is easy enough:
return $this->redirect($this->generateUrl('testnumber'));
However, I want to route to: testnumber/1/question/4. How can I accomplish this incredibly simple task? The only thing I have found in the documentation and Google allows me to add parameters and not just create a complex route. For example:
generateURL('testnumber', array('testid'=>1, 'question'=>4))
makes a URL of /testnumber?testid=1&question=4, which I do not want.
Edit: Yes, I already have the route created in a YML file. I simply cannot generate the URL to link to it.
return $this->redirect($this->generateUrl(???????????),true));
This is my route:
#Route("/testnumber/{testid}/question/{question}", name="testnumber")
The Symfony documentation only shows how to generate a URL to " testnumber/1", I need to generate "testnumber/1/question/4".
For
generateURL('testnumber', array('testid'=>1, 'question'=>4))
to work as you want, your route must look like (example using annotations)
#Route("/testnumber/{testid}/question/{question}", name="testnumber")
If you don't define "testid" & "question" parameters in your route, they'll be added to the query string (appended at the end of the URL as GET paramaters)
generated_route?test_id=X&question=X
Find here more relevent examples.

Page.GetRouteURL in WebForms outputs "length" querystring param

I'm working in a webforms app that uses routing in .net 4. I've defined a very basic route in global.asax as follows:
RouteTable.Routes.MapPageRoute("myRouteName", "MyRoutePath", "~/RouteHandlers/MyHandler.aspx");
In the codebehind of one of my pages I'm using GetRouteUrl to generate a the URL for this named route as follows:
Response.RedirectPermanent(GetRouteUrl("myRouteName"));
This doesn't produce the expected result of http://sitename/MyRoutePath. Instead it produces http://sitename/MyRoutePath?length=15
The length parameter doesn't seem to hurt but I've spent a lot of time making the URLs look nice so I don't want to see an extra parameter there. Any idea how to disable it?
I encountered this very same issue with just one of my routes using Web Forms this morning and I've got around it by providing a 2nd argument to the GetRouteUrl method, passing in null (as this particular route didn't require any route parameters).
For example:
GetRouteUrl("name-of-my-route", null)
My Url is now clean and is not appended with ?length=15.
Hopefully this might help your situation too.

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.

What is the name for that thing that lets part of the URL be an argument?

For example:
http://stackoverflow.com/questions/698627/ms-access-properties
The number is part of the URL but is an argument to the web app as opposed to other options like:
http://www.google.com/firefox?client=firefox-a&rls=org.mozilla:en-US:official
where all the args come after the '?'. I have used the second form before and I'm only trying to learn about the first form.
I'm sure I can find what else I need once I known what that's called so I can Google it.
URL Rewriting, generally.
Edit: Here is a good introduction to URL Rewriting.
Variables passed in the form of a URL are called the Query String. In a url like:
http://examples.com?a=b&c=d&e=f
The query string is ?a=b&c=d&e=f
In the Stackoverflow example, it uses URL Rewriting, specifically with MVC Routing to make 'pretty URLs'. There are other ways to do it in other languages. Some make use of Apache's mod_rewrite (example) while others parse the requested URI. In PHP a url like
http://example.com/index.php/test/path/info
can be parsed by reading $_SERVER['PATH_INFO'] which is /text/path/info.
Generally, they are using URL Rewriting to simulate the query string however. In the Stackoverflow example:
http://stackoverflow.com/questions/698711/what-is-the-name-for-that-thing-that-lets-part-of-the-url-be-an-argument
The important parts are the questions/698711. You can change the title of the question with impunity but the other two parts you cannot.
It's usually called the 'path info'.
That's just URL mapping. It lets you use pretty URLs instead of a large query string.
I believe the StackOverflow URL works that way because it is using MVC whereas your bottom example is using standard requests.
It is indeed done by URL rewriting.
Usually, web application frameworks do this automatically if you install it correctly on your server.
Check out CakePHP as an example.
It's called a URL parameter and uses the HTTP GET method. As others mentioned, it can be rewritten using URL rewriting so that the URL is easier to read and use. Some search keywords: "SEF URLs", "Apache Rewrite", "pretty URLs".

Resources