ASP.NET/SharePoint master page issue - c#

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

Related

Show default page to all (including unauthenticated) users

Here is the situation: there has been an application for years. It's old and should not be used. Therefore I created a whole new application to replace the other. Now I want to show a piece of information to the users trying to access the old one (regardless whether already authenticated or, which is more likely, not yet authenticated) and a link to the new one.
I don't want to change anything in the old one or just change as few things as possible, it should stay. So I came up with an idea: why not backing up current Default.aspx and then creating new Default.aspx telling users to try and use the new application (maybe slightly modifying web.config but how?). However, when an unauthenticated user enters ~/Default.aspx, she gets redirected to the login page.
Is it possible to allow unauthenticated users to see this default page without getting redirected to the login page just to give their credentials and then see the default page telling that there is the other application?
Are you just looking to "announce" that users should go to the "new" application (and not do an auto-redirect)?
IF SO, then wouldn't a simple html page that made this announcement be your "linker"?
The reason I'm suggesting .html (instead of an .aspx page) is that if you "don't want to change anything" in the old site, then an html page/file takes it out of the "ASP.Net pipeline" (asp.net will not process it, IIS will) - I'm guessing that the entire old application requires authentication because you mentioned the default.aspx (normally the default document of the web site/application) already requires a login (which is why it redirects).
You can set the "old" application default document to be this (new) standard "announcement" page.
Hth...

Asp.net page to return blank page with one word

in order to corporate with external payment system, I need to respond with blank page with one word 'true' at the beginning.
I'm using Dotnetnuke 6 and c# .net 4.0 custom module.
I must work with data sent by them, and later put just at the beginning of page world 'true'. So I could somehow reset page and return null page with one word or insert it just at the beginning of the page (even before tag )
I cannot redirect to another page, it must be the same one.
Any ideas ? Is it possible to obtain ?
If you haven't returned anything yet you can do Response.Clear(), Response.Write() and a Response.End() to end the request so nothing else is returned.
This sure sounds like a web service to me. I would either use DNN Services Framework. Or if it is really as simple as returning the word true, I would write an .ashx to do the job. The .ashx will not be run with other modules or as part of default.aspx, so you can easily control everything that is rendered in the response.

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.

Display ELMAH error detail in my own page?

I'm using C# and MVC 3.
I have a page where I can view a grid of errors that ELMAH has logged. I created this instead of using the elmah.axd page because I wanted the page to be styled the same as the rest of my administrative section.
That worked great, but I'd like to do the same thing for the error detail page. I like the way the ELMAH detail page looks, but it's its own page, and I'd like it wrapped in my styles and integrated with the rest of my admin section.
Is there a function within ELMAH that will render the HTML for an error like on elmah.axd/detail, so that I can throw it in one of my pages?
I'm not sure if there's a function, but you can always pull the data out of the storage container (xml file, SQL Server, etc) and display it however you like.

Getting text after URL in asp.net / URL Rewriting (sort of!)

My app is a very simple "one page" type app-
It has Default.aspx
I'm basically trying to get, for example:
www.myappurl.com/this is my text
I want to get hold of "this is my text" from the above example.
This will be displayed on the page (for now)
I didn't really want to have to use any complext url rewriting things for this...
(My hosting provider uses IIS6)
I tried using a 404 handler, but this is a bit long winded, and i'm using shared hosting, that can't set the "execute url" on custom 404 pages.
Any other ideas?
You can add a mapping for all requests with the * extension to the ASP.NET isapi dll (GET/POST) verbs. You will need to uncheck the "verify file is on disk" checkbox when mapping the extension in IIS. (In IIS7 integrated mode, you map the extension in the web.config as well). Note that this will caause everything to be served by asp.net, even images and script files, which can slow things down.
Then create a handler mapping in your web.config to a http handler you create.
From there, in the ProcessRequest() method of the handler, you have access to the HttpContext that spawned the request and can manipulate the URL from there.
That is the easiest option, you could also create a HttpModule, or have the default page at root redirect to http://www.domain.com/default.aspx/this is my text, in the code-behind of default.aspx, you will be able to get the text following the page and slash.

Categories

Resources