I have a C# application to access data from a third party website. I'm using WebClient and I call DownloadData and process the bytes(DownloadFile would yield similar results).
This worked fine until recently. Now, more often than not, instead of the desired XML file, I get an html page similar to:
<!-- _localBinding -->
<!-- _lcid="1033" _version="" -->
<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8" />
<meta HTTP-EQUIV="Expires" content="0" />
<noscript>
<meta http-equiv="refresh" content="0; url=/_layouts/spsredirect.aspx?noscript=1" />
</noscript>
<script language="javascript" src="/_layouts/1033/init.js"></script>
<script language="javascript" src="/_layouts/1033/core.js"></script>
<script language="javascript">
var requestedUrl = escapeProperly(window.location.href);
STSNavigate("/CustomErrors/ErrorPage.aspx?oldUrl=" + requestedUrl);
</script>
</head>
<body></body>
</html>
I'm stumped as to why this went from consistently working to consistently giving me problems. Sometimes, I can still get the expected XML file. Do you have an idea of what this issue might be and how I can fix it?
It might be worth noting that if I were to click the download link manually in a browser, a javascript window would pop up and close before the file downloads. I'm using WebBrowser to intercept the non-static URL of that popup and pass that link (and headers/cookie information) to WebClient. The link would look something like https://foo.bar/Something.axd?Session=1234vv1234Format=XML
Thanks for your time
It looks like the third-party page is throwing an exception. This could be due to parameters you are passing it, or it could be a problem on their end. The best course of action would be to contact the provider and see what errors are being logged (hopefully they are logging them somewhere). Otherwise you are just going to be guessing at what you are doing wrong.
Is it possible that there are encoded\escaped characters in the session querystring param that are not being handled properly? That would be my first guess since there aren't any real error details.
If it's throwing an error, I'd attempt to collect the HTTP status code, you can do this by using the HttpWebRequest and HttpWebResponse (I believe these pre-dated WebClient). There's tricky ways to still use WebClient and retrieve the status code as well, I'd check the content type as well before you actually download.
In any condition, even if serving an error page, the server should respond with an appropriate HTTP status code. (At least in a perfect world).
Related
I have faced a lot of issues with CSS and JS caching, so i decided to add timestamps to CSS and JS files.
so i added this param at the end of links:
?t=<%= DateTime.Now.Ticks %>
So here how it looks like:
JS:
<script type="text/javascript" src="Scripts/global_scripts.js?t=<%= DateTime.Now.Ticks %>"></script>
CSS:
<link rel="stylesheet" type="text/css" href="Styles/mysite.css?t=<%= DateTime.Now.Ticks %>" />
The JS file work fine. But for CSS files this param ?t=<%= DateTime.Now.Ticks %> gets recognized as a string. Why? I cannot find the difference
You can easily solve your problem with this simple adjustement
<link rel="stylesheet" type="text/css" href="Styles/uberhint.css?t=<%= "" +DateTime.Now.Ticks %>" />
Just add "" +.
ASP.NET is treating the link as an HtmlLink control, and rendering the contents of the href attribute as a literal. Inserting that Extra string forces ASP.NET to accept that you are trying to generate a dynamic string
While this is not an answer to your original question, I can suggest you to use a different approach altogether. Instead of adding ticks, you can use default Microsoft libraries to make bundles. When you do it that way, the engine will automatically add a string like ?v=dfsdf9fsdfasd which will be a hash of the content, so every time it changes it will be different. Then you can just cache forever the resources forever on the client.
Your approach is problematic since the browser will download js every time, this is extremely inefficient.
You can get some details on how to use bundles with minification e.g. here. Please see my answer for some details on how to make script bundles properly.
P.S. If you don't want to do bundling yourself, you can still implement the hash of the file content yourself. Another easy option is to do ?v=1, where you get the version from config, then by changing the config, you will be able to force cache update on all you clients.
With all of the approaches you should set cache for 1 year (max if I remember correctly).
If the above answers dont work, you can try to add a
<asp:placeholder runat="server" id="ph">
above script and css tag, and in the page_load you can bind placeholder.
ph.DataBind();
I have a ASP .Net MVC4 Web application. In it I have my usual html for the _Layout.cshtml, which in turn loads the default Home/Index. All works fine.
In my index I am also loading a partial view. This works fine too. No probs.
I am using a the UI tools from the following site:
http://www.keenthemes.com/preview/index.php?theme=metronic
The problem is it seems to be primarily HTML4 and not designed for MVC out of the box so I am having to tweak it slightly to get it to work the way I want. (Nothing beyond anything very basic). For example, moving one part out to the index and using Renderbody() to load it so the actual html structure never changes. I have done this a million times to be sure I am not missing any closing tags or anything else that could cause my problem.
Up to this point there is no problem at all. Everything loads as it should.
I continued to create a 2nd View and its partial to extract other parts of the site. As usual, baby steps first. Before extracting any other code, I just used a little "Hello World" in the first page, and a similar string in the partial to be sure it was working. It was.
Now when I type in the url Home/ActionName the whole thing reloads as it should but looks horrible. and I get this error message:
0x800a1391 - JavaScript runtime error: 'jQuery' is undefined
Below is my code which clearly defines it:
<!-- BEGIN CORE PLUGINS -->
<script src="assets/plugins/jquery-1.8.3.min.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function ()
{
App.init(); // initlayout and core plugins
_Layout.init();
_Layout.initJQVMAP(); // init index page's custom scripts
_Layout.initCalendar(); // init index page's custom scripts
_Layout.initCharts(); // init index page's custom scripts
_Layout.initChat();
_Layout.initDashboardDaterange(); //Red date range
_Layout.initIntro(); //Pop up messages
});
</script>
It points me to the jQuery(document).ready part when I see the message.
Again, when I load the page normally, it works fine. When I type Home on its own it works fine. Its only when I type Home/AnythingElse that it gives this error message. Even if I type Home/ which should load in the Index file, it gives me this error message.
jQuery is defined, so why is this happening on postback?
Any help is appreciated.
Try setting the src for jQuery to be absolute from the site root:
<script src="/assets/plugins/jquery-1.8.3.min.js" type="text/javascript"></script>
Note the / before assets - when your src path does not start with a / the browser will try and load the asset relative to the current path, so in your example when you add the trailing slash to Home it will try to load jQuery from Home/assets/plugins/...
For me, the MVC bundlers were causing problems (in my case the ui bundler)
Here is the order which worked for me.
<script src="/Scripts/modernizr-2.5.3.js"></script>
<script src="/Scripts/jquery-1.7.1.js"></script>
<script src="/bundles/jquery-ui"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
You need to add the jquery-x.xx.x.js first before the validate.js like this
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
I have done lots of reading up but I don’t understand why this is not working. I am using VS2010 with current updates. I want to Load a Web Page into the C# Web Browser Control (This Application is a Windows Forms Application):
this.webBrowser1.Navigate("https://mydomain/L1/L2/L3/L4/webpage.asp");
this.webBrowser1.PerformLayout();
In my understanding, to call Javascript Functions from C#, one would use:
this.webBrowser1.Document.InvokeScript("passwordReset");
or if one had Parameters to pass:
object[] parameters = new object[3];
parameters[0] = “1”;
parameters[0] = false;
parameters[0] = “3”;
webBrowser1.Document.InvokeScript("passwordReset", parameters);
My Web Page code is as follows. I have shown both methods I have tried. One seems to work, if I insert the Javascript into the page.
I don’t want to work with this method due to limitations I have. I need to be able to call the *.js File as shown:
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>My Test Page...</title>
<script type="text/javascript" language="javaScript">
function passwordReset()
{
alert("You're about to change your password!!");
}
</script>
<script type="text/javascript" src="/L1/L2/Password_Reset.js"></script>
</head>
Please note, I am not including both Javascripts in my page at the same time. I have only included both in this example.
Can someone please explain why this method, when calling on functions in the “Password_Reset.js” file, is not working as desired?
I have tried setting full permissions: [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
and also making this app Com Visible: [System.Runtime.InteropServices.ComVisibleAttribute(true)]
EDIT: What I am trying to achieve, is to be able to run Javascripts after webBrowser1.Navigate("URL"); when they are written into the page code as Follows:
<script type="text/javascript" src="/L1/L2/Password_Reset.js"></script>
EDIT: In Re-Reading my question, I wonder if this issue is a Folder Level problem and the Script is not loading? Can someone advise on this?
The solution was, in the end, very simple, and a little on the obvious side. I am a little embarrased.
Once the Browser Control Document Completed:
webBrowser1.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(webBrowserTest_DocumentCompleted);
Event has been called, and when:
webBrowser1.PerformLayout();
has been run, the Javascripts, at this stage has not fully loaded from what I can see. If I set a delay timer up and count a 1000 milliseconds then call my Javascript, the whole thing works perfectly. This is unless, by delaying the calls to my javascript, are in-turn allowing another issue in the background to resolve itself?
I guess the next thing to solve, is why is the DocumentCompleted eventhandler not including the loading of scripts in the way one would expect.
I hope others find this usefull and it saves them the time I spend resolving ths issue.
I'm not sure of what you are trying to accomplish.
However, I suggest you to look at startup script registration methods. If you use them, a JavaScript function will be executed in client browser after html document loading.
Take a look at this article. Maybe you can use it solve your problem.
In my master page in the Page_Load method, I have this line:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Basically, I don't want to cache the page. I do want to cache the .js and .css files but when I reload the page, these files don't get loaded from the browser cache but instead get reloaded.
What do I need to fix?
Thanks.
Add these lines in the html meta section
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT="0">
<META HTTP-EQUIV="PRAGMA" content="NO-CACHE">
in ASP page
// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// Stop Caching in Firefox
Response.Cache.SetNoStore();
This article can be usefull to resolve your issue
http://www.codeproject.com/Articles/11225/Disabling-browser-s-back-functionality-on-sign-out
P.S. If it didn't help. You can use cheat like this: <%= here you should add some random number for example Ticks %> - add it for elements which shouldn't be cached.
For example:
<link rel=stylesheet href="main.css?<%= ... %>">
You need to set up caching for static resources (.js and .css) files. This is not enabled by default.
See this SO answer for more details: https://stackoverflow.com/a/2196848/69868
Also note that when you are reloading the page using F5/Ctrl+F5, some browsers will ignore caching instructions and they will always fetch fresh content (for example Firefox and Ctrl+F5). If you want to test caching, reload the page by re-entering the URL in location text box.
I have a small asp.net mvc app running on WIN2k3 and IIS6.
I'm using a wildcard mapping under the application settings in IIS as described in this article http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx . The application works fine under my local asp.net development server. When I publish from VS2008 out to the production server above the styles are looking partially rendered. For instance. I have an overflow: hidden; set on the body {} and the browser is showing a scrollbar horizontally and vertically. Also, the z-index on all my layers are incorrect. Any ideas would be much appreciated.
Here's a sample of my document head.
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>page title | <%= ViewData["PageTitle"]%></title>
<link rel="shortcut icon" href="/content/images/misc/favicon.ico" type="image/x-icon" />
<%= Html.MooFrameworkConfig("Default")%>
<%= Html.JavaScript("/scripts/framework.js")%>
</head>
framework.js includes the CSS like so:
this.loadCSS("layout.css", "screen");
here is the loadCSS method:
framework.loadCSS = function(fileName,mediaType) {
var headID = document.getElementsByTagName("head")[0];
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = '/content/skins/' + skinBase + '/css/' + fileName;
cssNode.media = mediaType;
headID.appendChild(cssNode);
}
Just as a test, does linking to the CSS file in the HTML (as opposed to loading it with JS) fix the problem?
It is unlikely to be a problem with the web server, per say. Is it possible that the stylesheet you're using isn't being served by the webserver? Maybe the URL to it is slightly different? Check by navigating manually to the stylesheet or by checking that it is being loaded in firebug.
Did you include the CSS fix from the article?
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
I think you may need to post snippets from your website where you include the CSS file and where you simulate the article so that you can get better tips.
You can use Fiddler tool to check if all the files are loaded correctly, I mean no HTTP 403 or 500 or some other status codes for all the files (.css,.js) included in your page.
There maybe some server settings preventing displaying your page correctly.