Access System.Web.Http.Current.Response From A Within A Thread?

Jul 16, 2010

I have a ASP.NET page with a WebMethod that creates an object and runs one of the object's methods in a new thread.I need to access the System.Web.HttpContext.Current.Response.ContentType property from within this thread. Each time I (try to) access it, I receive a NullReferenceException.

View 1 Replies

Similar Messages:

Localization :: Assign Culture To Current Thread When Http Is Stateless?

Feb 5, 2010

When I was localizing my web application I got one strange doubt.

In the first request i set current thread's current culture to user chosen culture. It worked fine for that particular request. But when i browsed for other page, server rendered content in default english culture. I think the reason meight be "http is stateless" means current request is served by a thread whose culture was set properly. Send request will be serverd another thread which does not about previous thread. So we should once again assign culture to this thread. Localization can be done by setting culture to a session variable.

So my question is what is the benifit that we get by setting culture to current thread. With setting to thread we can show the page in user desired language.

View 2 Replies

Access The Current Thread's Context Using HttpContext.Current?

Mar 11, 2010

I have a static class with serveral static methods. In these methods, I'm trying to access the current thread's context using HttpContext.Current. For example:

var userName = HttpContext.Current.User.Identity.Name;

However, when I do that, I receive a NullReferenceException, the infamous "Object reference not set to an instance of an object."

View 3 Replies

Web Forms :: Access Values When Returned Via HTTP Response

Feb 15, 2011

im working with a 3rd party on a project, we are going to pass them up to 3 values via a querystring and they are going to return 2 values that i need within a http response.

I found a few example here and just using google but they all varied of course due to the individual requirements in those examples..

what im looking for is a example that can get me started on how to send them the values during a button click on my page, wait for a response from them, then read the 2 values returned and continue with my button event which will involve passing the values to our stored procedure.

I found this link [URL] but not sure how i would call that within my page or if this would even work for what i need.

they should have the page ready sometime today so i can see how the data is being returned and so on.. figured i would see if i can get something setup so i can just plug in values when they are ready.

View 3 Replies

C# - Difference Between Http.context.user And Thread.currentprincipal And When To Use Them

Jun 16, 2010

I have just recently run into an issue running an web app under visual studio 2008. I get the error 'type is not resolved for member...customUserPrincipal'. Tracking down various discussion groups it seems that there is an issue with Visual Studio's web server when you assign a custom principal against the Thread.CurrentPrincipal.

In my code, I now use...

HttpContext.Current.User = myCustomPrincipal
//Thread.CurrentPrincipal = myCustomPrincipal

I'm glad that I got the error out of the way, but it begs the question "What is the difference between these two methods of setting a principal?". There are other stackoverflow questions related to the differences but they don't get into the details of the two approaches.

I did find one tantalizing post that had the following grandiose comment but no explanation to back up his assertions...


View 3 Replies

Asynchronous HTTP Handler And Using HttpContext In A Background Thread?

Feb 10, 2010

I was reading Walkthrough: Creating an Asynchronous HTTP Handler and noticed they pass the HttpContext from the handler thread and use it in a WaitCallback which runs on a background thread. It makes calls like _context.Response.Write(). Am I correct in assuming that this doesn't violate the fact that HttpContext is not thread safe because the handler thread will not be using it after the async work has started?

Also, Using an HTTPContext across threads has some good information about HttpContext and threads. Is all of HttpContext not thread safe, or just items like Response? Can multiple background threads access the Items property, if only in read mode?

View 1 Replies

C# - Set HttpContext.Current.User From Thread.CurrentPrincipal?

May 17, 2010

I have a security manager in my application that works for both windows and web, the process is simple, just takes the user and pwd and authenticates them against a database then sets the Thread.CurrentPrincipal with a custom principal. For windows applications this works fine, but I have problems with web applications.

After the process of authentication, when I'm trying to set the Current.User to the custom principal from Thread.CurrentPrincipal this last one contains a GenericPrincipal. Am I doing something wrong? This is my code:



View 5 Replies

Cross-thread Operation Not Valid: Accessed From A Thread Other Than The Thread It Was Created On

Apr 2, 2010

I want to remove checked items from checklistbox (winform control) in class file method which i am calling asynchronously using deletegate. but it showing me this error message:-

Cross-thread operation not valid: Control 'checkedListBox1' accessed from a thread other than the thread it was created on.

i have tried invoke required but again got the same error. Sample code is below:


View 1 Replies

Getting Website's URL Without HttpContext.Current (running In Background Thread)?

Aug 3, 2010

Bit of a long shot, but is there a way in ASP.NET to dynamically get the website's URL [URL] when there is no HttpContext.Current available.

There is no HttpContext because the code is running in a background thread* (but under the ASP.NET AppDomain). I have a background process that sends emails out every evening, and needs to include the Web address, but I don't want to hard code it because of deployments and testing (it changes from [URL] to[URL] and then to [URL] for the live site).

View 2 Replies

What Is The Result Of Setting The Current Thread's Culture Code

Jun 2, 2010

what is the result of setting the current thread's culture code?

I understand if I use the resource file it will pull label's/strings from the .resx file.

What else? Will it effect my date/money formatting also?

View 3 Replies

C# - Accessing HttpSessionState (HttpContext.Current.Session) From Another Thread?

Nov 25, 2010

We have a web site which implements a central HttpSessionState management in App_Code like this:


All of this worked fine ultil we needed to implement a time consuming process in a new thread...
In the second thread HttpContext.Current.Session is null (we know its because the current context its different between threads) so everything fails :

Investigating we found that you could pass the session from one thread to another like this:


View 2 Replies

Finding Difference System.web.httpcontext.current.application And System.Configuration.Appsettings?

Jan 22, 2010

Can anyone tell me the differnce between




View 2 Replies

Force This Logic To Use The CultureInfo Attached To The Current Thread Instead Of The InvariantCulture?

Nov 18, 2010

I have an ASP .NET Web Forms application that makes use of the 'WebMethod' attribute for making AJAX calls from jQuery. I'm dealing with trying to localize the application so I recently created a web method that looks something like this for testing purposes:


I have an HttpModule that is setting the 'CurrentCulture' and 'CurrentUICulture' properties of the current thread to 'pt-BR' (Brazilian Portuguese) at the 'BeginRequest' event.

Client side, I have a jQuery AJAX call to this 'HandleDate' web method that is providing the dateValue parameter as '18/10/2010'. In the 'pt-BR' culture this should evaluate to October 18, 2010 (day/month/year date format).

When I execute this I'm getting back an error indicating that the 'System.Web.Script.Serialization.ObjectConverter' is blowing up stating that '18/11/2010' is not a valid value for DateTime. The stack trace included with the error indicates that this was thrown by the 'System.ComponentModel.DateTimeCoverter.ConvertFrom' method, which accepts the object to be converted in addition to a CultureInfo object representing the culture that should be applied during the conversion.

I fired up Reflector and it appears that the 'ObjectConverter' is invoking the 'DateTimeConverter' using CultureInfo.InvariantCulture instance, which I think is the problem.

How can I force this logic to use the CultureInfo attached to the current thread instead of the InvariantCulture?

View 1 Replies

Web Forms :: System / DocumentCompleted Not Fired Inside Thread

Oct 1, 2010

Yeah some people would say "Are you crazy using winforms controls inside asp forms"... and I think they are right. But I would say.. "I'm not the only one!!, take a look" [URL] So. Doing some kind of stuff like the previous link. I did the following:


Now. the DocumentCompleted event is not fired (neither ProgressChanged) and I've tried the next: Add the library MsHtml.dll to my project and place the file into my lib folder... I did it... No changes.Try to handle the state of the WebBrowser.ReadyState... I did it... No changes (Actually after receive the ebBrowserReadyState.Complete I tried to print the document with


View 1 Replies

Web Forms :: Creating A Messaging System; Messages In A Thread?

Jan 13, 2010

Im creating a messaging system abit like the inbox feature of facebook and even this website (its gonna be created as a website). Now the messages ive been told should go to a database and then from there be displayed on a page. Im gonna be creating this system because its a project for university. Its not going to be published online or anything, just a simple demo in front of suoervisor n examiner. Im gonna simply create a database within the application to hold all the data.Now i need to have user login feature capability so the right messages are displayed to the right users. I wants the messages to be as a "thread". Any idea how this can be done in im using visual studio 2008 .net framework 3.5.

View 1 Replies

C# - MP3 Download In An HTTP Response?

Jun 9, 2010

I've a question about something I'm searching for,for too long! We've build an application from which an admin upload songs into a database. Then user can bought songs and download it individualy. The problem is that when user download MP3 songs with the code below, it works great in Firefox and Chrome but not in IE8 simply because WMP trying to open the songs and it just don't get it instead of having a "Save as" dialog? Any issue on HOW can i force to have the "Save As" diaglog? Note that I have not MP3 physicaly on server it's in database. So I can't direct link to song ...

Here's my code :

// Remove "specials chars"
foreach (char aChar in @"/:*?""<>| ") {
if (aChar == ' ') {
songNameAndExt = songNameAndExt.Replace(' ', '_');
} else {
songNameAndExt = songNameAndExt.Replace(aChar.ToString(), string.Empty);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.Headers.Add("Content-Disposition", string.Format("filename={0}", songNameAndExt));
HttpContext.Current.Response.OutputStream.Write(songData, 0, songLength);

View 1 Replies

HTTP Response And Request In Binary

Feb 21, 2010

I need to I can make the web request to a webservice which take a XML argument. And is expected to return a Binary response. I am able to make the request but while recieving the response back I am unable to get the response in binary. When I read the response
using streamreader see the header and some attached "HEBRISH" words probably binary but unable to sepreate it out. Please help in seprating out the binary the response data.

View 1 Replies

Web Forms :: Get Response Value - From Http Post

Mar 23, 2011

What wrong with this code?


I would like the numeric values of the enum of Response.StatisCode

View 1 Replies

Custom HTTP Response C# And Ajax?

Apr 4, 2011

I'm trying to query a database that contains information about a "ticket" using jQuery's .ajax() method.

type: 'GET',
url: 'Preview.ashx',
data: 'ticketID=' + ticketID,
success: function (data) {
// 'data' should be a row from the database, so it should be like an
// array that contains each column of the row
// do stuff with this data
... so that all works fine. I'm having trouble with the data variable. On the server side, I do...
// get the ticket ID from the POST parameter
int ticketID = context.Request["ticketID"] != null ? Convert.ToInt32(context.Request["ticketID"]) : -1;
if (ticketID >= 0) {
// grab the data from the database, getInfo() will retrieve the row
// in the DB that corresponds to the ticket ID given, returning an
// ArrayList with all of the information
ArrayList theTicket = getInfo(context, ticketID);
// now, I need to somehow return this information so that I could deal with it
// in the 'success' callback function above
} else {
// something went wrong with the 'newTicket' POST parameter
context.Response.ContentType = "text/plain";
context.Response.Write("Error with 'ticketID' POST parameter.

I've debugged this enough to be sure that the ArrayList contains the correct information. Now I just need to return it. How would I do this? How would I return the data in the ArrayList? Is it possible to structure the response so that I could do data.ID, data.otherColumnName, etc... in the callback function to access the different fields?

View 3 Replies

Suppressing HTTP 500 Response Codes

Feb 10, 2010

I've had a bit of feedback from some threat and vulnerability folks relating to websites returning HTTP 500 response codes. Essentially the advice is that all possible measures must be taken to avoid the server throwing a 500 (i.e. extensive form input validation) which is fine. However, the advice also suggested that attempts to compromise security by means such as inserting a tag into a random query string causing ASP.NET request validation to fire or manipulating viewstate also should not return an HTTP 500. Obviously the native framework behaviour is to interpret the request and possibly throw to a custom error page but even this will return a 500 response code.

So I'm after some thoughts on how to approach this. Is there any way to configure the app at either the .NET level or IIS level to return an HTTP 200 when a 500 is raised? Or does this become a coding exercise at global.asax level in one of the application events? Are there other consequences to consider? BTW, the rationale from the security side is that apps which return HTTP 500 may be viewed as "low hanging fruit" by bots randomly scanning for vulnerabilities and prompt further malicious activity. attempts I'm personally not convinced that changing response codes offers any real security gains but am happy to hapy to take the advice of the pros.

View 3 Replies

Response - Get Custom Http Header?

May 14, 2010

I have an appliction on the one server. There I've added code on server-side in Page_Load:

Response.AddHeader("key", "password-key-from-hotel");

On the client side I have a form:
<form ... action="" >
<input type="hidden" id="asd" value="fgh" >
<script type="text/javascript">

Then on the other domain - there is also my other application - I'm trying to get the hedaer "key" by this code:


But there is no such header. Is there is a desicion? Where is my mistake?

View 2 Replies

Add Response Http Headers In Web Configuration?

May 27, 2010

In my application I need to set a http response header. I'd like to do this in web.config. but I dont'know if this is possible and I can't find it on Google. Solution Finally, after a long search I found the solution. Create a class with this code:

public class myHTTPHeaderModule : IHttpModule
#region IHttpModule Members
public void Dispose()
public void Init(HttpApplication context)
context.EndRequest += new EventHandler(context_EndRequest);
void context_EndRequest(object sender, EventArgs e)
HttpResponse response = HttpContext.Current.Response;
response.AddHeader("Content-Language", "*");

(Don't ask me why to use this event, but it works..) Now add a line in web.config in the HttpModule section:

<add type="namespace.myHTTPHeaderModule, assembly name" name="headers" />
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

View 4 Replies

C# - Exactly Does Response.Redirect("~/...") Put In The HTTP Response?

Nov 24, 2010

I've just finished reading URL vs. URI vs. URN, in More Concise Terms, and it's really helped understand the distinction between the three terms. Since then I've skimmed the RFC2141 and RFC2616 specs and Microsoft's Response.Redirect Method documentation in an effort to answer the following question confidently.

Given this line of code:


And this resulting HTTP response (trimmed for context):

Status=Found - 302 Date=Wed, 24 Nov
2010 17:27:58 GMT

What name(s) most properly describes what has been placed into the "Location" header?

URL? URI? URN? URC? Which is it?

View 5 Replies

Creating Dynamic Http Response From Component

May 14, 2010

I've developed a component which processes online payments. At a specific point the user must be redirected to the selected acquirer web site for authentication. The acquirer itself suggests that there should be a web page which does it. Something such as the following,

<html xmlns="[URL]">
<head id="Head1" runat="server">
<script type="text/javascript">
function Go() {
<body onload="Go();">
<form id="form1" runat="server" method="post" action="/...">
<asp:HiddenField ID="MID" runat="server" />
<asp:HiddenField ID="Amount" runat="server" />
<asp:HiddenField ID="ResNum" runat="server" />
<asp:HiddenField ID="RedirectURL" runat="server" />

This page redirects the user and posts required arguments to the acquirer's web site. Prior to this page there is another page with a payment button (for example). In the click event of the payment button my component is instantiated and required calculation is done. Something such as the following,

protected void btnPayment_Click(object sender, EventArgs e)
var paymentProcessor = new NS.PaymentProcessor(/*required arguments*/);
NS.Result result = paymentProcessor.Calc();
Session.Add("PaymentProcessResult", result);
catch (Exception p)
throw p;

What I'm going to do is to generate [TakeOff.aspx] page at run-time by the payment processor component instead of having static page (I mean aspx pages that are generated during development phase). The reason why is that we may have different acquirers and each acquirer requires its own arguments to be posted. So if the payment processor component generates aspx or even html pages at run-time we do not need recompile and redeploy the web application.

My problem is that I do not know how to generate aspx pages at run-time. I'm not an ASP.NET guru and all efforts that I've made using HttpResponse were in vain as the original page does not transfer the control to the generated page.

View 1 Replies

HTTP Response Behavior - Cause Browser To Prompt

Jan 5, 2011

I have two very similar pieces of ASP.NET code that send a file in an HTTP Reponse to the client. They should cause the browser to prompt to save the file. The first one works, the second one doesn't. The HTTP responses as seen in Fiddler are below.

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 228108
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
content-disposition: attachment; filename=Report.xlsx
Date: Wed, 05 Jan 2011 12:17:48 GMT
<binary data>

Not working:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/
Date: Wed, 05 Jan 2011 12:19:21 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 228080
content-disposition: attachment; filename=report 2.xlsx
Cache-Control: private
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Connection: Close
<binary data>

When the first one is seen in Fiddler the browser correctly prompts to save the file. When the second one is seen in Fiddler, nothing observable happens in the browser. Same behaviour in both chrome and firefox.

EDIT: ASP.NET code that produces the second response

Response.Buffer = false;
Response.ContentType = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-length", genstream.Length.ToString());
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", filename));
byte[] buffer = new byte[1024];
genstream.Position = 0;
int n;
while ((n = genstream.Read(buffer, 0, 1024) ) > 0)
Response.OutputStream.Write(buffer, 0, n);

View 4 Replies

Copyrights 2005-15, All rights reserved