URL routing in asp.net - asp.net

Ok. I hope it's a right question and title.
I'm totally newbie to asp.net and c#. I've been searching the answer for days and got nothing. So i think this is the best place to ask this question.
this is my current URL
http://localhost:7474/mywebsite/ProductDetails.aspx?Category=CSR&artID=36&alias=support-for-central-jaya-and-yogyakarta-earthquake-disaster
I Want to make search engine friendly url. Can I make above URL to
http://localhost:7474/mywebsite/ProductDetails/CSR/36-support-for-central-jaya-and-yogyakarta-earthquake-disaster
FYI I'm not using MVC.

Yes, you can.
Take a look at RouteCollection.MapPageRoute for details but roughly you need the code below:
routes.MapPageRoute(
"ProductDetails",
"/ProductDetails/{Category}/{artID}/{alias}",
"~/ProductDetails.aspx");
However artId and alias cannot be in the same segment. They need to be separated by a slash (/).
The other difference is that you need to grab the parameter values from RouteData instead of querystring so instead of:
Request.QueryString["Category"]
you do:
Page.RouteData.Values["Category"]
ps: I did it from the top of my mind so it might not compile

May be you can try URL Rewriting. That would be an ideal choice to implement this with no code change at all. And use regular expression to extract values from the url to bounce the values to original aspx page.
http://www.iis.net/downloads/microsoft/url-rewrite

Yes, we can create search engine friendly URL using ASP.NET routing feature. This blog post have an example of URL routing -
http://newapputil.blogspot.in/2013/12/aspnet-40-web-forms-url-routing.html

Related

Best way for ASP.NET MVC5 localization where locale is passed in querystring with attribute routing

A very long title, sorry for that.
I'm looking for your input on the best way to support localization in an ASP.NET MVC 5 project in which i would like to pass the locale as part of the querystring. This way it would be easy for users to share a link to website and also pass the correct locale as part of the link.
If this would be done using, for example cookies, the user would pass a link but the page might be in a different language for the person who receives the link. I don't think that that is very nice.
The solution i currently use (http://afana.me/post/aspnet-mvc-internationalization.aspx) does not work nice with incorrect urls and just keeps loading the same page in loop. There is a lot of information on the web and i already went through a lot but i would like to know what is most commonly used and really works well.
As a bonus i would like the solution to work with attribute routing as my current solution doens't play nice :(
you can use resource files for that, the current culture is in the current thread of the web request, so no need to pass it in the querystring but it can be done;
have a look at this live demo: http://prodinner.aspnetawesome.com
you can download/read pdf about it here: http://prodinner.codeplex.com
you can see there that you can change the language using the dropdown;
in Global.asax.cs Application_BeginRequest, there's already code in there related to language, it reads a cookie now (or it's absence), you can make it read the querystring

ASP.NET website translation

I want to translate my ASP.NET website, just like microsoft does.
I want the urls to have /en-EN/ or /de-DE/
the problem is that my urls ar already routed URLs
How can I do this?
I found this http://blog.maartenballiauw.be/post/2010/01/26/Translating-routes-(ASPNET-MVC-and-Webforms).aspx
but I don't want to translate all the urls and this method seems to me too complex, with Views and Controllers.
I need mysite.com/en-EN/routedpage or mysite.com/de-DE/routedpage
The above example is for mvc, is your website in mvc?. You need to setup your website for culture specific setting, here is an article how to do that
http://www.ezzylearning.com/tutorial.aspx?tid=3477182
Also for further reading
http://www.codeproject.com/Articles/7998/Creating-multilingual-websites-Part-1
and
http://www.codeproject.com/Articles/8073/Create-multilingual-web-pages-very-easily
its very easy to setup
This is an important problem. You want the URL to have the language encoded in it for both SEO and usability purposes.
Two common solutions are:
1) Change the domain. fr.example.com or de.example.com. Then the routes don't need to be changed.
2) Use the # for the language code. Search engines nowadays will observe this. So, then you'd have www.example.com/#de or www.example.com/#fr
You can implement both of these solutions with www.SiteTran.com by simply pasting our javascript into your page and setting the sitetran_url_type variable to either:
var sitetran_url_type = 'sub.domain';
or
var sitetran_url_type = 'hash';
SiteTran is 100% free and is my company.

Change page names or do a URL Rewrite?

I would like to change the page names of a ASP.NET site to make them more meaningful and more SEO friendly. My new page names with be more descriptive (ex: My_SEO_Friendly_Page_Name.aspx) My question is, is it enough to change the page names for SEO or do I have to implement the URL rewrite.
What are the differences of one over the other?
I think either method workds.
home/blog/this_is_my_entry_page_title.aspx
or
home/blog/this_is_my_entry_page_title
You mainly want to do URL rewriting to avoid using query string parameters. So instead of:
home/products.aspx?qid=25
You would want to use:
home/products/Cell_Phone
Instead of My_SEO_Friendly_Page_Name.aspx use My-SEO-Friendly-Page-Name.aspx for better SEO.
Search engines consider _ (underscore) to be a character and (-) minus sign to be a word-break. More words - more keyword matches.
the result for SEO is the same, it's the implementation that's different.
when you implement url rewrite, any request to http://domain.com/My_SEO_Friendly_Page_Name.aspx will be served with e.g. http://domain.com/index.aspx?id=12 where the 12 refer to the page id in the database, and the page in question has the title 'My SEO Friendly Page Name'
so, this approach is more suitable to implement on large sites which store it's content in a database.
The search engines cannot tell the difference. So one method is not better than the other as far as SEO is concerned. If you implement Url Rewriting, IIS hides the fact My_SEO_Friendly_Page_Name.aspx actually gets mapped to Default.apx?id=1234. To the outside world, it is the same as a page named SEO_Friendly_Page_Name.aspx
If you can use .net 3.5 sp1, don't use rewrite. Use routing.
Routing with ASP.NET Web Forms
One issue you may have is if the existing pages are already indexed by the search engines, if you throw up a whole load of more pages which are in fact just the same then you have a lot of dupe content on your site. To avoid the dupe content a sitemap may help to some degree, you can instruct google to delete pages, or to ignore certain url parameters, which would then delete your old pages that use a url param, or you can get your app to http 301 the old pages to the new one.
You should use 301 redirects, if possible. You can do this in a .htaccess file, however for a large dynamic site it's probably better to use ASP to handle it.
So if you original page was index.aspx?page=52 then your index.aspx script would look up the ID of 52 in your database, find the appropriate "slug" or "alias", say My_SEO_Friendly_Page_Name and set the headers etc in ASP to redirect to that URL.

Can you determine the name of the route followed from your webforms page?

I have a ASP.NET 3.5 webforms project I have enabled routing over.
I also have in the project a number of controls doing different things based on what page they are currently being shown in. It would seem that the most straightforward way to control this different behavior is to discover which route was used to load the page and then do things according to that.
However, I can't seem to find a way to discover the route bar looking at the actual request URL and running a regex over it which isn't great. Does anyone know a way to look it up some other way?
Update: there still doesn't appear to be a way to do this in ASP.NET 4.0. Hopefully someone else has figured this out?
It looks like Phil Haack has answered this question in a blog post on his site: http://haacked.com/archive/2010/11/28/getting-the-route-name-for-a-route.aspx
In a .NET 4 webforms app, I used this to determine the route definition.
string myOperation =
((System.Web.Routing.Route)(Page.RouteData.Route)).Url;
//string has value "Stop" or "Start"
Let's say your routes are like so:
routes.MapPageRoute("StopEmailAlerts",
"Stop/{SomeToken}",
"~/Emailing.aspx", false);
routes.MapPageRoute("SendEmailAlerts",
"Start/{SomeToken}",
"~/Emailing.aspx", false);
I posted a couple of simple extension methods you can use to get/set the route name on this post. Seems simpler (to me) than Haack's solution.

Asp.Net Url Handling

Is there a built in method to handle urls like Default.aspx/mycontent or do I need to handle it myself by taking the url and stripping of the file's path?
I have tried searching for it but haven't been able to find anything.
I'd like to handle .aspx/parameters and am not looking at Mod/URL Rewrite.
You could either write an url rewrite handler, use ASP.NET MVC routing in your webforms application, or use ASP.NET MVC instead of webforms.
Take a look at ASP.NET MVC. This framework obviously goes far beyond just "user-friendly" URLs, but it does also handle this as a byproduct.
Or you could just write a HttpFilter...
If you want you're app to do "friendly urls" then surely you would want to avoid ".aspx" appearing in the URL? Have considered ASP.NET-MVC or at least the routing elements of it.
You can parse out the appended "folder" using the Request.Url.Segments Array:
this.Response.Write(this.Request.Url.Segments[this.Request.Url.Segments.Length - 1]);
Then use Server.Transfer or render whatever you like. You will often have problems with relative paths and such for CSS and the like.

Resources