Server.Transfer doesn't update all paths - asp.net

Using Server.Transfer to show a page that informs the user that the web site is at maintenance mode.
At global.asax:
void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.IsLocal)
return;
if (ConfigurationManager.AppSettings["MaintenanceMode"] == "true")
{
if (Request.AcceptTypes != null && Request.AcceptTypes[0] == "text/html")
Server.Transfer("~/UserMessage.aspx?Maintenance");
}
}
Works well except when looking at the page source code I see that the CSS path has been updated but images' paths are not.
Any suggestions?

I would use app_offline.htm in the application root or at the very least Response.Redirect if I were you, Server.Transfer does not change the HTTP address, so you have to be careful redirecting all assets to the underlying page or make all addresses absolute

The server.transfer is actually stop the execution of a page and start execute a different page, the page that you give it.
The result is that the user see a different page under the same url.
This have nothing to do with image path. Also the images are not pass from asp.net except if you have set even images to pass from asp.net processing. Also on the code that you have give us you do not make any transfer if they are images, but you check for the text/html.
Now if you have move the execution to a different css, the image path is not change, only the page that the user see change.
Maybe you mean that you use the MapPath() and this is not change according to the new directory ?

Related

Uploading large file just returns blank page in asp.net

Suddenly, my ASP.NET web application is returning completely blank HTML pages after you upload overly large files. (Like a 6MB file - I've set the various request lengths and file upload settings to limit it to 5MB).
The page is really blank, View Source in chrome reveals nothing, completely empty. In IE, it's a "This page can’t be displayed" error.
Stepping through the code, it's clear that the codebehind of the upload buttons (which executes when the file is small) is never executed. The blank page comes up instantly as soon as the file is uploaded.
I put a pretty standard Application_Error() method in Global.asax and it catches the error you'd expect (HttpUnhandledException.ErrorCode = -2147467259) and it can Server.Transfer() the user to my own custom error aspx page, I can even hit a breakpoint in the page load of that and step through it, but once it's done... I just get the blank page.
Anyone have any ideas? Something I could try?
ASP.NET terminates processing the request if its length exceed allowed limit set in maxrequestlength.
If you want to catch the exception, try to get it as explained at
How to catch ConfigurationErrorsException for violating maxRequestLength?
Another way is to set maxrequestlength higher than the required limit and check file size in the code
protected void button_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile && fileUpload.FileContent.Length > 5*1024)
{
// too big
}
}

Default page not rendering with Site.Master content

I have a hosting account with GoDaddy.com, IIS 7 server running .NET 4.0, and I am in the early stages of developing a web site for our church. The content is a free CSS based template I have moved into an ASP.NET Web App with Master pages. (If critique on content is necessary please keep in mind this is a very early stage of development...but I am open to any suggestions. :) )
For some reason, when I enter the full URL to the default page, the page renders properly. However, if I only enter the folder name without the page name, I only get the content form the page itself.
See for your self:
http://www.websmithsllc.com/lpacftp/Home.aspx
http://www.websmithsllc.com/lpacftp
I don't think this is an issue with my wire-up between the content page and the masterpage as it will properly render when I use the full URL. Therefore, I assume the issue is in one of three areas:
How I am publishing: One Click to an FTP directory
The project settings: Currently Home.aspx is the start page
An issue with the settings on my host.
I really hope the issue isn't #3 because my experience so far has been that their tech support is severely lacking in the area of Visual Studio / IIS development and publishing.
Now, some additional clues. I KNOW that the Site.Master file is being rendered, at least to some extent. The menu that is being displayed is created in the Site.Master.Page_Load event handler:
protected void Page_Load(object sender, EventArgs e)
{
//Load sidebar content
Page p = HttpContext.Current.CurrentHandler as Page;
menuContent.Text = Helpers.StaticHelperMethods.GenerateMenuContent(p.Title);
}
Static method:
public static string GenerateMenuContent(String pageTitle)
{
StringBuilder menu = new StringBuilder();
if (pageTitle == "Home")
{
menu.Append("Home\n");
}
else
{
menu.Append("Home\n");
}
More similar code...
In this case, p.Title should == "Home", but the code is responding as though is does not, and I don't know how (if I can) debug live to see what's going on. Finally, if you look closely at the second link, you'll notice some stock ASP.NET advertising text- that appears to be coming from the stock "Default.aspx" file in the BodyContent asp:Content object. However, looking at the properties/Web tab I can see that the startup action is Specific Page : Home.aspx.
So- hopefully I haven't added a ton of unnecessary info here, but at least enough for someone with more experience to help me figure out what I'm doing wrong here.
Thanks in advance for whatever help you can offer me on this.
You need a default page. Create it and then in the code behind in Page_Load write:
Response.Redirect("Home.aspx");
Or change your default page in IIS. Or change your home page to Default.aspx (and rename the class and the page directive).
Of the three, creating a Default.aspx page that redirects to Home.aspx is likely the easiest.
Yes, MatthewMartin is correct. Your hosting service's IIS is not configured to pick up "Home.aspx" as a default page. You will need to either get them to add it to the IIS configuration, or rename your home page to Default.aspx, or create a "dummy" Default.aspx that redirects to your Home.aspx.

Response.Redirect is redirecting to a page that doesn't exist

I'm doing a simple check for some user data that get's put into session on sign in. What happens, is I click through a few buttons to get to this page. This page a has a drop down that is set to autopostback=true.
When I change my selection in the drop, my request get's redirected to a page that doesn't exist. signin.aspx exists in the root folder of the site. The attempted redirect looks for signin in the folder that this particular page is in (example.com/folder1/signin.aspx) instead of example.com/signin.aspx.
Should I be using something other than Response.Redirect to accomplish this?
Side note about the application:
This is .net 4 using jquery 1.6.4 and jquerymobile 1.0. I'm thinking jquery mobile is the problem because I use this same pattern/practice on other applications without issue.
Location of page where this is happening.
example.com/folder1/page2.aspx
location of sign in page: example.com/signin.aspx
url that displays in the error. example.com/folder1/signin.aspx
protected override void OnInit(EventArgs e)
{
if (Session["UserData"] == null)
{
Response.Redirect("../SignIn.aspx");
}
}
You should always use asp.net style root-relative paths:
Response.Redirect("~/SignIn.aspx");
That makes the URL relative to your site's root (not the web root unless your site is the web root), but still allows the site to be moved around.
jquery wouldn't be affecting a server side response.redirect. Are you sure that the page exists as it's being set in your code? If signin.aspx is in the root of the website, could you not simply do this?
Response.Redirect("/SignIn.aspx");
Try This
protected override void OnInit(EventArgs e)
{
if (Session["UserData"] == null)
{
Response.Redirect("~/SignIn.aspx");
}
}

Why do I lose my javascript from the browser cache after a full page postback?

I have an external javascript file which I include to my page on the code behind (as seen below).
My problem is, when I my page makes a postback (not partial one), I check the loaded scripts by using FireBug, and I cannot see the javascript file in the list after the post back. I asusmed once it is included to page on the first load, browser will be caching it so that I do not need to re-include it.
What am I doing wrong?
The way I include the script is here:
protected override void OnInit(EventArgs e)
{
if (this.Page.IsPostBack==false)
{
if (this.Page.ClientScript.IsClientScriptIncludeRegistered("ctlPalletDetail")==false)
{
string guidParamToHackBrowserCaching = System.Guid.NewGuid().ToString();
this.Page.ClientScript.RegisterClientScriptInclude("ctlPalletDetail", ResolveUrl(String.Format("~/clientScripts/ctlLtlRequestDetail.js?par={0}",guidParamToHackBrowserCaching)));
}
}
base.OnInit(e);
}
The browser will be caching it to save it from downloading again on postback.
But you still need to register to script on the page to tell the browser to actually USE the cached script into the page.
Or in other words: registering the script on the server will render a <script> tag on the served page that will tell the browser to actually use that script file.
Wether the script is cached locally or not on the client doesn't have any bearing.If the script is not already cached then the browser will download it. If it is cached, it will not use it unless you tell it to (via the <script> tag)

Passing value through querstring without showing the destination page in the URL with ASP.NET web form

I have a small web app being written in ASP.NET, VB.NET , .NET 3.5
I pass a value from default.aspx to demo.aspx using query string. When Go button is clicked the URL will be like this : localhost/demo.aspx?id=4
But I want URL to be sth like this when Go button is clicked : localhost/?id=4 and the id value is passed to demo.aspx so the expected result can be shown. How to get this done if possible without using the routing technique.
This will work only if the web server considers demo.aspx to be the default document. When you load a page without stating the page name (such as you localhost/?id=4 example), the web server will locate a file with a pre-defined name and use it as the default document. Often this file is called default.aspx (or .asp, .htm, .html, .php or something like that). So if you want to load any other page you will need to either state the name, or use URL rewriting techniques.
Modify your go button to link button and set herf = "?id=[id]"
Modify Page_Load event of Default.aspx by using the following code.
public Page_Load(object sender,EventArg e)
{
if(!string.IsNullOrEmpty(Request["id"]))
{
// Using Server.Transfer() function to call Demo.aspx page.
// Client still see "localhost/?id=[id]" at address bar.
Server.Transfer("Demo.aspx?id=" + Request["id"]);
}
}
Something like this?
protected void btnGo_Click (Object sender, EventArgs e)
{
Response.Redirect("localhost/?" + Request.QueryString);
}
Not sure when you want the url to change? Not sure what the routing technique is either. Sorry.
if you want it to work with postbacks, you still need to make sure the webserver is configured with a default document like default.aspx.
in 3.5 sp1 MS finally allows us to set the action attribute of the form.
so on page init you can explicitly set it now
Form1.Action = "/mydirectory/"
Caveat: Some versions of IIS do not allow posting to a directory. (XP IIS for example)

Resources