ASP.NET - Who can use one web application and "host" many sites - c#

I've a personal project, and the logic layer and database is equal, but I've two domains that the only thing that changes is the URL, and the master page.
What I need is a simple system to recognize the URL and show the correct site master, because I what use the same database with the configuration of ASP.NET tables, stored procedure and so one.
The most similar thing I've saw, it's the DNN portal system, but I don't need all the features, like separate login system.
Thanks in advance.

I think something along these lines will get you what you need:
In IIS, point both URLs to the same virtual directory, so they are both being served from the same place -
Add a PreInit event to read the URL then load a MasterPage accordingly:
void Page_PreInit(Object sender, EventArgs e)
{
if(Request.Url.AbsoluteUri.Contains("mysite.com"))
{
this.MasterPageFile = "master page file goes here";
}
}
Hope that gets you in the right direction!

You could make the master page dynamic. The host name can be found in:
HttpContext.Current.Request.ServerVariables["SERVER_NAME"]
You can use this inside the master page to change its output.

I would take it one step further and abstracting everything away that differ and then use dependency injection to resolve what you need.

Related

How to rout the URL in asp.net

I have a page in my asp.net application, using this page with an number of links to open it, now I have a new version of same page with different name and extra functionality.
I need both page should be serve by same links depend upon user permission, suppose user "A" have permission to access old version of page then he should get old one and if user "B" has permission to access newer version of page then he should get new page, the page access permission logic will be derived from database.
I have an idea to do it by using a common page that will be linked with every link in application, under that common page I can check which page access current user has and redirect him accordingly, but I do not want to change every link in my application to change the old page link with common page.
I need some concrete functionality of asp.net application like URL routing.
Please help if any one have any Idea on this.
On the Page_Load of your login page, you'll want to check if the user is authenticated, and if they are to redirect them to your access denied page:
protected void Page_Load(object sender, EventArgs e)
{
if(!User.IsInRole("A"))
{
Response.Redirect("~/NewPage.aspx");
}
}

Multiple Master pages for multiple solution Issue[master pages can create from one particular system]

I have multiple solutions for one asp.net website project. each solution refers different master page. Master pages located in some other solution. we call the master page in page
protected override void OnPreInit(EventArgs e)
{
MasterPageFile = clsVirtualPathProvider.LoginMasterFileLocation;
base.OnPreInit(e);
}
The problem here is, the master page created is not from my system. its created from other system. if i will create and use new master page,then it will work in my system. not in other system. In others system it will go to 500 Internal server error. is there any solutions pls...
Solved this by doing following:
1. go to particular master page right click and select properties
2. change Build action to Embedded Resource

Redirecting url to index.aspx page using standard asp.net3.5 and web.config

I have a subdomain that is http://trade.businessbazaar.in . I am dynamically creating urls from database something in this manner http://trade.businessbazaar.in/mycompany. To display details, I have an index.aspx file there,thinking that on every request the index.aspx page will load and display data accodingly. Also, There is a masterpage on the index.aspx page from where i am capturing the text mycompany and query it in database to fetch result. But nothing seems to work.
A genuine link is http://trade.businessbazaar.in/Symparlife. But its unable to load index.aspx. I need a clean approach without any third party dll or rewriters. Directly to push some lines in config and start working. That is url will be the same but index page will get loaded...
In short, i want to say
I need the StackOverflow type clean url mechanism to fetch pages
Thanks in Advance
You can handle the Begin_Request event in Global.asax and add custom code to redirect to index.aspx and convert the parts of the URL into query string arguments. You should use Server.Transfer to keep the URL in the browser.
I'd recommend upgrading to 4.0 and using the Routing enine though. You should check if the standard routing is available as a download for ASP.NET 3.5. I am sure your code will get messy very soon. Been there, done that.
As #Mike Miller mentions in the comments the Routing engine ships with ASP.NET 3.5. You can check the documentation here - http://msdn.microsoft.com/en-us/library/system.web.routing(v=vs.90).aspx
Here is a tutorial on how to use it with Web Forms - 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
For your case the code would be something like:
routes.MapPageRoute("company-index", "/{company}", "~/index.aspx")
And in index.aspx you can access the route value for company like this:
string company = (string)Page.RouteData.Values["company"];
Keep in mind that you'd better add something in the URL before your actual argument (the company name). If you don't you will have problems later on when because you may want to add a URL like "/Login" but then you will have to validate that users can't create a company named "Login". Not how Stack Overflow has "/questions/" before the actual question info in the URL.

ASP.NET/SharePoint master page issue

I am using SharePoint Server 2007 + C# + .Net 3.5 + VSTS 2008 + ASP.Net. And I am using collaboration portal template.
I am developing a custom aspx page and put it in _layout folder of a site and I want to apply default.master of the SharePoint site to this aspx page. Any samples about how to achieve this goal?
You can put code in the PreInit event to make your custom page use the current site's masterpage.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
SPWeb myWeb = SPControl.GetContextSite(Context).OpenWeb();
string strUrl = myWeb.ServerRelativeUrl + "/_catalogs/masterpage/my.master";
this.MasterPageFile = strUrl;
}
Or replace my.master with default.master in order to use the default master page. Or check spweb's MasterUrl property and use that instead.
Either way it should get you going in the right direction.
If you use SharePoint Designer then you can right-click on your page and then select a Master page to apply.
The best way to do this is by use of an HttpModule. This enables the use of your custom master page for all application pages (i.e. pages in the LAYOUTS folder). It can be deployed using a feature and can be activated per web application (seeing as the httpmodule needs to be registered in the web.config it is web app scoped.)
By making it web app scoped, your end users will have a uniform user experience, instead of that single page looking like the front end of the site while all the other (Out of the box) application pages are still using the default sharepoint application.master.
For a code example and a more in depth explanation, look here.
P.S. You are getting errors using the code above because of missing content placeholders. You need to create a copy of your custom master page. Although styling can be the same, application pages use more/other ContentPlaceHolders than a front end master page.
Just copy your custom master page, rename it from CustomMaster.master to, say, CustomMasterEdit.master and use that for application page styling, SharePoint will throw an error telling you which placeholders are missing, keep adding the needed placeholders till the page works (there are 2 or 3 extra placeholders needed i believe).
P.P.S. To make sharepoint display errors that make sense, go the web.config and look for the <SharePoint> tag and change the callstack attribute from false to true. Then, look for the customErrors tag and set the mode attribute to "off". To completely enable debugging, you can also enable ASP.NET tracing. Of course you should NOT do this in your production environment....
More info on modifying the web.config to make sharepoint display real error message can be found here.
and

Different websites on different domains, one .NET MVC application?

Is it possible to have one .NET MVC application, and have it accessible from different domains, in such a way that the content will be domain-dependant?
For example, both www(dot)site1(dot)com and www(dot)site2(dot)com will point to my server's IP, and to the same website in IIS. In that website my .NET MVC application will reside. Now, I want the ability to know which site (domain name) triggered the ControllerAction, and act accordingly (for example, display different content for the homepage in the Index action, or allow/prevent access to specific content assigned to a specific site).
I would appreciate any help on this. I can accept an extra parameter passed to all controller actions (probably using Routing), but if there's a more elegant solution that would be ideal.
Well, you can always get the domain from the Request.RawUrl property.
As Mercer mentioned, deploying these as two separate web apps would be a better solution though. If that isn't possible, I would try to design something relatively generic that would check the domain and return different Views for each domain.
I have written a blog post about how to do this with an example web application for download.
It uses an abstract base Controller that is aware of the site it is being called for - by creating controllers that inherit from this base class you have automatic access to the current "site" for the current request.
It also allows you to load all your sites from a single database - can save you a bit on hosting fees if you're on a shared host, or if you run your own server you don't have to set up a new database for each site you create.
You can easily access the domain name used in the request with something along the lines of the following:
switch(Request.ServerVariables("SERVER_NAME"))
{
case "www.site1.com":
//do something
case "www.site2.com":
//do something else
default:
//????
}
You can do this in anywhere you have access to the Request object.
An elegant solution would be to have 2 deployments for 2 domains, and to separate content.
You could still have common content, but separating the content without hardcoding this inside the application is a win situation.
If you use different databases to keep the data separate, then in the Session Start configure the application to use one of the databases based on the Server Name variable. Then place the working connection string in the session for the user.
protected void Session_Start(Object sender, EventArgs e)
{
NameValueCollection NVCSrvElements = Request.ServerVariables;
switch (NVCSrvElements.Get("SERVER_NAME"))
{
case "www.whatever1.com":
Session["ConnStr"]="db1 connection string";
break;
case "www.whatever2.com":
Session["ConnStr"] = "db2 connection string";
break;
}
}
Then use this connection string in the rest of the application.

Categories

Resources