asp.net 4 url routing, how to access the route? - asp.net

I'm using ASP.Net 4 URL routing on a web forms site.
I have multiple routes to a single page
routes.MapPageRoute("","our-services", "~/Information.aspx");
routes.MapPageRoute("","our-company", "~/Information.aspx");
On the destination page (Information.aspx) how can I tell which route was used to get there, for example was it from our-services or our-company?

You can try with
HttpRequest.RawUrl
it should contain the original URL that was called (i.e. before the rewriting)

From the information page, you could check who referred to it via Request.UrlReferrer.
HTH.

Related

Access same website with multiple sub names

I have developed an asp.net application. I will host the application as a new website in IIS.
I want the application to work like below,
http://localhost:81/firm1/login
http://localhost:81/firm2/login
All the above 3 calls need to hit my website and I will do some process with the names 'firm1', 'firm2'.
I don't want to create multiple websites/virtual applications or sub domains.
You can use redirects from one domain to another by creating an on click event. But the best way to do it would be with Routing as mentioned in the previous answer. This is because hyperlinks generated using Routing does not require the changing of URL if the page changes its name or location.
Following this will help you to effectively use Routing:
1. Define custom URL patterns that are not dependent on physical file names.
2. Generate URLs based on route URL parameter values by using markup or code.
3. In a routed page, retrieve values passed in URL segments by using markup or code.
To add routes to a Web site, you can add them to the static Routes property of the RouteTable class by using the RouteCollection.MapPageRoute method.
Have a look at Routing in asp.net applications.
This will allow you to write modules that will receive the url of the requested page, break apart the url in components and delegate to your controllers from there.
If you already have routing set up, you will need to go through all the routes that need to sit below a 'Firm' and modify the URL they map to. For example if you have
routes.MapPageRoute("LoginRoute", "/login", "~/login.aspx")
You would change that to
routes.MapPageRoute("LoginRoute", "/{FIRM_CODE}/login", "~/login.aspx")
The routing classes will parse out the first part of the URL and make it available inside your controller as
RouteData.values("FIRM_CODE")
From there you will need to modify all your logic to handle the firm specific behaviours. You will probably want to store this value somwhere in your session state so that libraries have access to the current firm etc.

changing page address format

I create a website with ASP.NET web form and I used URL friendly to shape my URLs , by URL friendly my URLs change but not enough for example one of my page "تور استانبول" URL after changing by URL friendly is Domain.com/tours/تور-استانبول/استانبول/1 but I want Domain.com/تور-استانبول/استانبول/
please note that I don't want to query on sql to rewrite to my page and also I don't want to create static page and because my pages are dynamic I can't write all of them in web config file
please help me in this case
Hamid, you can use asp.net routing. The following link contains explains how to do it:
https://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
hope it helps

ASP.NET Web Forms URL Rewriting capability

I have a very simple question, rather I am just curious.
I have about 2000 pages of the url format somewebsite.com/ShowProduct.aspx?ID=223 . This particular url has a page title as 'Walking sticks for elders made from durable steel'.
Can I use URL Rewriting to convert this to a url like somewebsite.com/walking-stick-for-elders ? Also will I have do it dynamically for 2000 pages or is there any expression that can be used?
You need to have some thing which uniquely indentifies URLs just like Stackoverflow does it.
ASP.NET Web Forms URL Rewriting capability
See this question URL - Where from URL you get its for question then there is a question id and then description. I suggest you do the same.
If you are using ASP.NET 4.0 or higher version then you can do that type of URL routing very easily.
Like from this URL - http://weblogs.asp.net/scottgu/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series
There is also a asp.net friendly URL package form where you can write URls.
http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx
RouteConfig.RegisterRoutes(RouteTable.Routes);
If each product name is unique and stored inside the database you can easily route each SEO friendly URL to productID without manually inputing a new route in the Routing table.

One custom handler for different request url's in ASP.NET web site

I am doing a small ASP.NET 2.0. site refactoring.
Currently there is the following folder structure:
/siteroot/services/home/service1
/siteroot/services/home/service2
/siteroot/services/home/service3
...
all the folders (service1, 2, 3) contain an almost identical default.aspx, with the different content hardcoded into the web form declaration.
My idea is to create one service.aspx and serve custom page (with the same template) on a particular URL request.
My question is:
How can I redirect all request to ../services/home/service1,2,3 to one particular handler/aspx page?
Preferably not to have those folders in the project structure at all, but intercept a requests that are headed to them.
If you are able to implement ASP.NET 3.5 (runs on the 2.0 CLR), then Routing sounds perfect for what you need to achieve. With this you can intercept URLs based on a particular format and then route them through to a single page or handler. You won't need a folder structure for each of your services.
Here's a few links on how to get going with it in ASP.NET WebForms. It's pretty quick to implement:
http://msdn.microsoft.com/en-us/magazine/dd347546.aspx
http://msdn.microsoft.com/en-us/library/cc668201%28v=VS.90%29.aspx
http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx
http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
One way:
Create a custom handler class that implements IHttpModule
In Init(HttpApplication application), register a method for application.AuthorizeRequest
In this method, parse ((HttpApplication)sender).Request.Path as appropriate, using HttpContext.Current.RewritePath if necessary to point to a "real" service.aspx page.
Then register your custom handler in <system.webServer><modules>.
Another way is to use the MVC-style routing, which works without MVC just fine. Details are in the answer to the question here: custom URL Routing in asp.net.

ASP.NET webpages without names, like stackoverflow?

Mentioned stackoverflow only as an example, but if you look above the URL for ask is
http://stackoverflow.com/questions/ask
which means /ask is a subdirectory, but they also do this for the specific question pages. How do you code this in .NET?
Not a code question as much as a technique. I know this is great for SEO, but how do you create a site so that every "page" is its own directory? Dynamically.
Do you have a template or a hidden redirect???
How?? :)
It's termed URL rewriting:
Url Rewriting with ASP.NET
MSDN: URL Rewriting in ASP.NET
EDIT: As #Justice points out, StackOverflow uses Routing.
StackOverflow uses something called Routing, which comes with .NET 3.5 SP1. Routing is a popular feature of a number of MVC frameworks, such as ASP.NET MVC, Ruby on Rails, and a number of Python and PHP frameworks.
Stack Overflow was built using ASP.NET MVC which uses a technique called Routing, see:
What Was Stack Overflow Built With?
and Routing
Stack Overflow uses ASP.net MVC
MVC uses the URL + Query String to determine the content, so its not like a URL which points to a specific page, but more like a hierarchical path to the properties of some data to be displayed
E.G. https://stackoverflow.com/users/[Put User ID Here]/[Put User Name Here]
prompts the website to display a USER with an ID specified in the path ( in this case the user name is probably just for kicks ) as opposed to a specific page created just for that user.
I have seen this accomplished by simply creating a folder for every web page and then having each folder contain a Default.aspx document (Assuming Default.aspx is setup as a default document in IIS, which it is by default). Then you can navigate to any folder on the site without specifying the page (Default.aspx).
For the dynamic part, I have worked with CMS systems that do it this way and the Default.aspx page simply inherits from some master template and the CMS system utilizes the ASP.NET rendering enginge to dynamically complete the web page.
Using folders may be a little heavy with the site structure, but it is an easy way to eliminate the page names from the browser.
This is how I structure my website and avoid having to use page names... for example http://www.innovaapps.net/Blog simply brings up the default.aspx page without having to specify the page name.

Resources