I have a page which contains a dynamic number of custom WebControls. What I want to do is get the containing page's query string via "Request.QueryString".
If I understand the problem correctly I need the containing page's HttpRequest object?
Is there a way to do this?
I probably should point out that I don't want to pass the QueryString from the containing page to the WebControl. I want to access the QueryString directly from the WebControl.
Consider the following link:
HttpContext.Current.Request.QueryString
You should be able to access the query string from a custom web user control (ascx) in the same way as you do from the page, i.e:
Request.QueryString...
From a custom control, you can either access it via:
Page.Request.QueryString
//or
HttpContext.Current.Request.QueryString
BTW: the last option (System.Web.HttpContext.Current...) also works from any non-web-control classes (e.g. business logic).
you can access the httpContext from anywhere using
HttpContext.Current
From there you can find the Request and the querystring
No need to to anything in particular, Request object is directly available also to webcontrols:
this.Request.QueryString
Related
Currently, our application has a lot of ActionLinks that only pass an id to the controller, where the object is retrieved again (that was already present/known on the previous page). Is there any cleaner way to do this, like pass the object instead? As our backend isn't too quick it takes a few seconds to load in the popup which you get after you click on the ActionLink.
I've seen some solutions using ajax to post the object but that doesn't seem like the neatest solution to paste those js lines under/at every ActionLink that is used in combination with a popup.
The #Html.ActionLink() method will generate a url link to the given Controller/Action. Thus, it can only contain parameters that can be contained in the url of the link. So you cannot pass an object through on the url.
If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).
Parameters in the ActionLink are set through the collection that you passed in as the third item in your function call above. Assuming default routing, this would give an address that looks like /Admin/EditPage/?name=XXX where XXX is the value of menuitem.Title. If you included something else here like itemId = menuitem.Id then it would add this as a query string parameter to the url generated, which would then be accessible to the action that is the target of this link.
I'm using the Google ReCaptcha element on a form. ReCaptcha adds a parameter to the POSTed parameters named g-recaptcha-response, which the controller needs to retrieve. Since this parameter name is not a legal C# variable name, it seems that the only way to retrieve its value is via Request.Params["g-recaptcha-response"] (rather than via model binding to a property in the view model).
Now, my problem is that elsewhere in the POSTed parameters I have a couple of form fields that may contain HTML markup. I have annotated the associated properties in my view model with [AllowHtml], which prevents the model binder from throwing an HttpRequestValidationException if the user enters HTML markup into the form. But [AllowHtml] apparently only works in the model binder. If the user has entered HTML markup into the form, then I get an HttpRequestValidationException when I reference Request.Params to fetch the ReCaptcha response.
As near as I can tell, the only way I can fetch the ReCaptcha response while still allowing HTML markup in selected POST parameters is to use go through the pain of writing a custom model binder (e.g. https://stackoverflow.com/a/4316327/1637105) to allow me to bind a property in my view model to an alias (in my case, a property name that is not a valid C# variable name).
The point of this question is just to confirm that I really do need to go to the pain of implementing a custom model binder.
Any suggestions or alternate solutions are more than welcome!
EDIT:
It occurs to me that another solution would be to figure out a way to fetch the value from the POSTed parameters without triggering (or while handling) the HttpRequestValidationException.
You can use the Unvalidated property of Request to access values without triggering Request Validation. for example
var captcha = Request.Unvalidated.Form["g-recaptcha-response"];
I am developing a web application in asp.net. I am using a class with property fields to store data. And I want access that property field in another asp page without using Session. Is there any way to pass the class object from one aspx.cs page to another (like query string). When I tried to pass it through query string and when I tried to access it one another page, I got the compile time error 'cannot convert type to 'class' type'.
You can do it two ways:
Serialize the object to a string and pass it through the query string (POST body better though)
Serialize the object into a JSON and pass it through the POST body.
I recommend to use the second option. However, you should reconsider the question, might be, there is better approach to your root issue.
For instance if I have http://www.mywebsite.com/about.aspx. Store about.aspx (or whatever page we're on) in a variable. Also need this to work even if there is information after the page in the url such as a query string.
Ah - I suggest you read the ASP.NET documentation VERY carefully. The HttpRequest object, available as Request property contains all information that you can have about the request. This includes the path called, all parameters etc.
Weighing in a little late, but others may get here as I did.
You could try to use a combination of the Uri and Path objects. For example:
Uri test = new Uri("http://www.microsoft.com/default.aspx?id=7&em=958");
Console.WriteLine(Path.GetFileName(test.AbsolutePath));
Not sure if this is the "proper" way but it works for me.
I am currently using a number of query string parameters to pass some data from one page to a second page (the parameters hold confirmation/error messages to display in the second page), that due to a third party product can no longer work correctly in the production environment. The user completes an action on the first page, and is then transferred to the second page. What are the possible alternatives to use instead of a query string and GET - session variables, POST data, or something completely different?
Thanks, MagicAndi.
You could create public properties in a source page and access the property values in the target page when using a server transfer. You could also get control information in the target page from controls in the source page by referencing the Page.PreviousPage property.
Both of these methods are oulined here: http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
Both POST data and session variables would work just fine. POST data has the drawback that it can be changed by the client and session variables take up memory, so you can choose based on that. I personally don't think that you should pass such messages to the client for the reason stated above but I guess you are already doing that, so...
you can use this if you use window.open("openTheotherPage",...etc)
so form the opened page you can do something like this
var valuefromCallerPage = window.opener.document.FormNmae.textbox.value
or button or anything on the caller page