How should I change the data source depending on the link? - asp.net

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.

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.

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

File download via external link - how to implement?

I have a web application which consists of many aspx pages ... one of them shows a grid with rows that can be exported to a file via button click. This works fine. Now I want to have that feature which allows a user to access an external link to this page (or another) and to export to a file and download. I dont need any information on the page, just the file download. How could I do this also including security features like encryption?
Thanks :)
The easiest way to do this is simply implement an HttpHandler that contains the logic to create that file and write it to the Response stream.
There are lots of examples of how to do this on the web that I won't repeat in this question. Just do a Google search for "Download File HttpHandler" and you should be golden.
One of the search results: http://www.c-sharpcorner.com/UploadFile/jhblankenship/DownloadingFromMemStreamHttp11262005061852AM/DownloadingFromMemStreamHttp.aspx
What you're going to have to do is when the gridview shows the correct rows to provide a 'unique link' which will be your website URL with url variables at the end. When the page loads it can check these variables and then use the database to look up the correct data etc.
Encryption in transit will be done via HTTPS (SSL), and to secure otherwise you would require a login to view the gridview / file.

Where content based websites store their content?

Sites like cnn.com or foxnews.com.
Where do they store all the articles? In html files? In database?
More logically to store everything in DB but how to generate a static link to something that is inside DB?
It's not that they have a a dynamic page load like: LoadArticle.aspx?ArticleID=123, every article has it's own address.
Please explain how this is done.
They use a special content management library called VoodooLib.dll.
Seriously, when you write something to a database, you normally generate some kind of unique identifier - 123, for example. It gets permanently associated with that record (article content). After that it is used to generate the same id as part of an Url at any time later.
As for the static link, it is a simple matter of Url Rewriting.
You generate static links to display on a page because they work much better for SEO. When a request for that static Url hits the server, it gets substituted for something "server friendly" and then gets to be processed.
They probably use some form of Content Management System (CMS). There are many different ones out there - most store the actual content in a database or as XML (some store XML in a database). They will the either publish that content as static HTML pages or, more commonly now, as dynamic pages that are cached. Many use what are known as "friendly URLs" that are virtual addresses that are mapped to the actual physical file path using URL-rewriting techniques.
Note you can't tell whether a page is dynamic or static simply from the extension. It is quite possible to have dynamic pages that end in the .html extension.
Just because the URL looks "static" doesn't mean it is; they could be using something like mod_rewrite or an IIS ISAPI to make the URLs more search engine friendly.
For the high-volume news sites that you mention, however, they may very well generate the pages statically in order to prevent overloading the database with repeated requests for the same article.
Look at the URl of this page, it doesn't have xxx.aspx?some-query-string
You are refering to using friendly URLs.
To do something like that, one common way is to use URL Rewrite and/or some custom HTTPModule
Here's a good reference: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Just because a page has a normal URL does not mean that it isn't serving dynamic content. With the Apache mod_rewrite module, it is possible to manipulate URLs. So, for example, a page like http://www.domain.tld/permalink/12345/message-title-slug can be converted internally to http://www.domain.tld/permalink/index.php?id=12345&slug=message-title-slug.
I do not know exactly what cnn.com and foxnews.com use, but I would bet that they use a Content Management System (CMS) which serves all pages dynamically, with the content stored either in a database or on the filesystem, and with authoring/publishing all being performed through the particular CMS.
Just checking cnn.com, the article links have in them
Year
Location (US or WORLD/specificlocationid)
Month
Day
Article name.
All of this information together can be used to uniquely identify any article (even less of it is probably actually needed). The dynamic content loading page address could easily be hidden by some method of URL rewriting, and then the information in the requested URL is used to determine which article in the DB is to be served up.
I don't know why all the other answerers seem to assume that some form of URL rewriting is necessary to create friendly URLs. It's not true at all.
It's perfectly possible to write web serving code that splits a URL into parameters - eg year, month, title - and pass that directly to the code that gets the content from the database, without any need to rewrite the URL. Most modern web frameworks such as Django and Rails include this functionality out of the box.
This is done through mod-rewrite techniques.
Here's an article about the mod rewriting engine: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
And here's their "guide": http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
I hope that helps. It should make for a good starting point. Goodluck.

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