Access same website with multiple sub names - asp.net

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.

Related

How can I redirect a user to a new page in IIS based upon the URL

I have a web app that opens certain projects based upon a projectID
So ID 12345 = project foobar
http://my.example.com?projID=12345
However I would like to provide a different site that doesnt require the projectid, and is easier to remember for the end user.
So if a user visits
http://simple.example.com/foobar (note this is a different site, though I suppose we could put this in the same site if easier)
They would automatically get redirected to
http://my.example.com?projID=12345
Obviously I would want a system where I could have more entries than just foobar
if you don't want to use the rewrite module mentioned above you could always do it yourself by implementing a custom 404 handler and key off aspxerrorpath or perhaps putting the code in a custom httpHandler.
We actually do this with in one of our applications. We use a cms system to specify all of the redirects so nothing is really hard coded and marketers and SEO teams can handle it without our help.
in my opinion you should use ashx to handle web request. here is very simple and beautiful example of what you are looking for :-
http://www.dotnetperls.com/ashx

How should I change the data source depending on the link?

I am creating a web solution that should run on two different web addresses. Depending on what link loads the site (ex www.tes1.com or www.test2.com) I want the website t use a different database.
I am checking for where the request comes from in the site master and I was planning to change the dbl file that all the web pages should use to another one. How can I do this? Is t even possible?
Yes its posible, you need to create a HTTP module where you check the context.Request.Url.ToString(). (its the current url for the website). Then you create a class with a static property called 'CurrentConnectionString'. You set the CurrentConnectionString in the HTTP module based an the url, then, for each connection to the database you use the CurrentConnectionString property.

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 4 url routing, how to access the route?

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.

Multiple domains, same web application on IIS 6

On IIS 6, is it possible to have multiple domain names pointing to the same web application, and conditionally serve CSS from within the web application based on the domain name?
I need to host hundreds of different "skins" on the same web application, with the skin being dependent upon the domain name, and I really don't want to launch tons of web applications.
+1 to rhinof for adding multiple identities, but creating a HttpModule is a bit over kill. You can simply switch the URL of the tag in a Master Page by examining the contents of Request.Headers["HOST"]
1) add the desired domain names as website identifiers in the advanced property page of the Web Site Tab.
2) map the .css extension to the aspnet_isapi.dll
3) write an httpmodule that will re write the url for .css requests based on the domain name
4) enable your module via the web.config
If you use themes, you can change the theme, thereby changing the css, etc. in the Page.PreInit depending on the value of the domain in Request.ServerVariables["Url"] (note, there might be a better server variable to get the domain name, look it up).
If you aren't using themes, you can programatically swap out the css file by checking the same server variable.
MasterPages are going to be your friend here.
Hope that shoves you in the right direction. It is possible and common.
If you are going to have different core content on the sites then I suggest putting in a global identifier to track which site a user is on and put your data in a DB somewhere for reference against that identifier. This is by far the easiest way to extend the app if each instance is unique.
You can put this into a class and have one common pattern for figuring out where stuff should map to. I suggest that once you know the mapping to cache that and then you will be able to do what you want without the latency of a thousand apps or db calls.
You will also need to add this parameter on any general DB calls so that you only get results for the domain that is being hosted. I’ve got a bit of experience with this so just leave some comments if you want to see some specific coding examples.
You can apply this technique to any file, CSS stylesheet or object for referencing purposes.
Yes, this should be simple to do. I'd go with the approach of mapping the domain names to your app using host headers in IIS. Then, as Martin said, interrogate Request.Headers["HOST"] in your app to switch the stylesheet.

Resources