Accessing a master page from httphandler - c#

I am developing a small application in asp.net (writing in c#).
In my application I am using jquery to perform asynchronous call to the server.
I have an http handler that listens in to the requests and does what it needs to do.
Problems start when in the handler I need to access information stored in the page , from where the asynchronous call started. When I try this:
Page page = HttpContext.Current.Handler as Page;
I don't get a page.
How else can I access the page itself?
Thank you

You have a slight design issue. The Page class IS an HttpHandler. It is in fact the default HttpHandler that handles requests. When you define your own HttpHandler, there is no Page class... and hence no Master either.
If you need to access information from a different page, you need to do that via the normal ASP.NET mechanisms... Session, Cache, etc.

You can create new instance of page.
SomePage page = new SomePage();

Related

Can we call Page_Load event in static web method using C# and ASP.NET

I have googled about this question but I didn't get the relevant answer so I want to know, is it possible to call a page_load event when the web method triggered ?
[WebMethod]
public static string AutoFillData(string entredsmbl)
{
// List<string> data = new List<string>();
try
{
// Once this event hit and I want to call the page_load event
// Page_Load(null, null);
entredsymbol = entredsmbl;
return entredsymbol;
}
catch (Exception)
{
throw;
}
}
Why I'm calling the page_load event ?
Because, instead of getting that returned data from static WebMethod and writing some logic in Ajax and load that data where I want (to avoid this process I want to call the page_load event).
I don't think so this is a good question but I want to know is this possible or not.
Give me your best suggestion
Well, lets say you cooked up some way to call that code?
You can't!!1
The reason why is that without a post-back, then all of the values of controls are NOT available.
A web method is static, and you don't even have to place that web method on the same page!!! All you are doing is calling a server side chunk of code. But, without a post-back, then all of the controls and their values are STILL setting on your desktop browser - not server side. So, even if you could call page load, without a post-back, then it can't change values of anything on the page - since the page is still setting on the users desktop - not on the server.
Remember, code behind and its value(s) does NOT exist until you do a post-back.
You can (even should) think of your web page code behind like calling a function or sub, and once you exit that function, then all values and varialbes have go out of scope.
The web server does NOT keep a active copy of your code behind page class in memory. The web server is wafting for you, or ANY other user to post-back.
when you post-back, THEN and ONLY then is a instance of your page class created in memory. Your code behind then runs, page is rendered, sent back to client AND THEN THE PAGE is disposed - removed from memory. Web server is now waiting for the next post-back.
So, you have this:
Note how your page class - code behind is NOT in memory on the server.
YOU DO NOT have this:
NOTE VERY careful here - the web page is ON CLIENT computer - it is NOT existing at all on the web server side.
And you DO NOT EVEN have this:
So, when you click on a button, or do a post-back, then you get this:
our web page is sent up to the server. You now have this:
So NOW your page is sitting on the server.
NOW a instance of the page class is created, and your code behind starts running.
Your code behind can modify controls (even controls to be visible or not), but the page is NOT interacting with the user - ONLY code can MODIFY the web page. So, one change, or MANY changes to the web page can occur, but AS YOU update things like a text box etc., the user does NOT see these changes just yet. So, you can't say run a loop to flash a text box on and off - since the changes are occurring on the server - the client side browser does not have the web page anymore!!!
It is THEN sent back to the client side, and the server side class instance and code is TOSSED OUT - DOES NOT exist!!! Your server side page class is disposed - removed from memory, and the web page code behind does NOT exist anymore.
So, page travels back to the client side, is re-displayed, JavaScript is loaded, and THEN JavaScript starts running.
Ok, so, with above in mind?
Then your question:
load that data where I want
You can't load or change or do anything to the web page , since the whole web page is still sitting on the client browser side - it DOES NOT exist on the server. You can't touch controls, or change the web page - it does NOT exist on the server side. That web page is sitting on the client side.
So, as above shows, it shows you don't grasp how a web page works. You can't call any non static method on the web page, since you don't have the web page on the server, do you?
If you need a few controls to be ONLY updated? Then drop in those controls you want to change + the button inside of a update panel. That will then post-back ONLY what is inside of the update panel, code behind runs, and then the update panel comes back to the client and only that part of the web page will update.
Keep in mind when using a up-date panel, that the page load event runs, but it does not make sense to have the page load do much, since again only controls in the update panel can be changed. But, page load does and will trigger each time.
With above in mind, then you can how and why your question does not make sense. The simple answer is that the controls and values are still sitting on the client side computer browser - and without the page being posted back to server, then the server has no means to update your browser.
The server can no more reach out to your web page on your computer anymore then it trying to reach out to some web pages on your computer doing your banking!!!!
If web servers could reach out to any browser page you have - and control that web page, then the web would be too high risk to use!!!
So, the server NEVER modifies anything on your desktop!!! You SEND it what you want to be change, and then code behind can modify that page now up on the server. When code behind is done, then the page is sent back to the client side for display.
I think update panel may well work for you case - but you need to grasp how the web works.

How can I run a check on page-load (server-side) on a website which includes both aspx pages and mvc?

I want to run a version consistency check between the website and database on every page in the software I work on to see whether one or the other is out of sync. (background: someone could upgrade while a user is using the software, so restricting the check to the sign in page isn't realistic - also why the check is required on any page in the software).
I am not in control of the deployment, as the customer hosts the software themselves on their own hardware.
The front-end is a mixture of asp.net pages and MVC4 (gradually replacing the aspx pages with MVC) , so I can't simply just run the check on Page_Load() in our inner and outer basepages and then have something different for our MVC pages - I would rather not duplicate code for each page type.
Having a look around, I have seen filters which exist for MVC which could be an option for those pages.
I've been investigating HttpHandlers and in theory could restrict the requests down to page load and not static content.
Is there an alternative/better way to do this server side check which would have the code in just one place and would affect both aspx pages and MVC?
Depending on what it should do when its passes the check or fails the check you could set up a new controller Version with an action Check
public class Version : Controller
{
public JsonResult Check() {
return new Json((GetWebsiteVersionNumber() == GetDatabaseVersionNumber()));
}
}
You can then call this endpoint from MVC using #Html.Action in _Layout or in another view and respond accordingly. On the Web Forms side you can then call this end point using the serverside WebRequest class and take appropriate action depending upon the response from your MasterPage PageLoad event or anywhere else you prefer.
Further you could call the endpoint from a common javascript file i(ncluded on both the WebForms and MVC client side includes) and using an AJAX request get the response and deal with it there also.
Excuse syntax errors as I was writing this off the top of my head.

how to send a request to one of the IIS handlers programatically?

i have a legacy 3rd party application which submits data to our internal sales system. It exposes ASP page with a form to the internet as follows:
<form id="ServiceRequest" enctype="multipart/form-data" method="post" action="AddToServiceRequest.csp">
where AddToServiceRequest.csp is a proprietary IIS handler:
Right now we embed this form into our ASP.Net 4 website using iframe - and that is really inconvenient. What I want to do is to replace this form with a native form, do all validation etc - and then call AddToServiceRequest.csp handler from code-behind logic. What's the right way to do it? I can think only about something like this:
var r = (HttpWebRequest)WebRequest.Create("http://localhost/AddToServiceRequest.csp");
r.Method = "POST";
r.KeepAlive = false;
// fill in form data
var res = r.GetResponse();
res.Close();
but it just does not look "right" to me. Are there any other ways?
If handler serving request is for some other site (from IIS point of view) than code for it will run in separate process or separate AppDomain and you will have no reasonable way to call it directly.
If handler is registered for the same site as yours you may be able to invoke it directly - i.e. if it is APS.Net class that handles request than it just an interface with couple methods - you may be able to instantiate and execute it directly. Note that many handlers depend on HttpContext.Current and you may not be able to set request reasonably for such calls.
It is also unlikely to register same handler to your site as most handlers/controllers/forms are designed to work for particularly configured site (i.e. Web.Config will have DB connection info).
So making direct web request is most straightforward solution. I would not try any other way as most web code will not handle unusual ways of invocation correctly.
You may consider HttpClient instead of WebRequest to get easier async supoprt (assuming .Net 4.5+), but any way of setting up request is ok.
Note that if site uses Windows Authentication you may not be able to pass user information via Web request .

Control web session using c#

I have a web portal that I'm trying to pass information to another website. When the user clicks on a link I'd like to load the new web page and stuff the two form fields with information then SUBMIT to automate a process.
I found this: Autofill 2 Fields on a Web Page with C#
However, this assumes I open a new webbrowser control but instead I want to use the current web session.
Is this possible or is there another way to accomplish this?
Thanks!
Use WebClient class. Place it inside onclick event of your link.
var client = new System.Net.WebClient();
string url = "www.mysite.com?a=xyz&b=123";
cli.Downloadstring(url);

Pass form variables on to second postback...asp.net

Is it possible to postback to the server, perform a function, and then continue that postback on to an external place? (ie, to a payment system)
(the scenario is clicking a button to place an order, mark it as sent, then send them off to the payment page (there are form variables that needs to be sent to the payment screen as well))
You can (probably) use Response.Redirect and send the posted variables to the external page as part of the querystring.
The variables will then be visible in the browser's address bar, but this is no less secure than posted form variables, just a bit uglier.
You need to ensure that the variables are tamper-proof regardless of how they're submitted to the payment page. You should consult the payment provider's documentation to figure out how to do this.
Instead of thinking "continue that postback", if you want to send these values to a payment page, you can store them in session state and access them on that payment page. It's not a "postback" if you're transferring control to a different page.
UPDATE
Since it's Worldpay payment service, you need to check their API and perhaps contact them.
Securely submitting form data to Worldpay using ASP.NET
Google Search on ASP.NET and Worldpay
Similar question on SO
Yes, you could do that. Once the postback loads, output an HTML form that occurs outside of the default ASP.net form, and use javascript to automatically submit that form once the page has loaded.
You could do this entirely as a javascript solution (and update the div outside the asp.net form) or you could overwrite the rendering method of the page itself.
A work around if the two are on different page. This is just skeleton
try
{
Response.Redirect(To your order page);
Process your data
try
{
Response.redirect to WorldPay
Do Payment
}
catch
{
to original page
}
}
Catch
{
Reposne.Redirect to origin page
}
I'd try to use the URL API to accomplish this. I'm not sure if this is the right part of the API though.
http://www.rbsworldpay.com/support/kb/bg/htmlredirect/rhtml.html#rhtml5207.html
Edit: I see they are passing the price on the query string. Hopefully that's not susceptible to manipulation....

Categories