In url rewriting, why is the postback giving the wrong url? - c#

I am experimenting with URL Rewriting. The first time it displays with the correct url. After perfroming any event handling, the form posts back, and then it has wrong url.
Like the page url is http://devweb.tsgdomain.com/nphnewdemo/Enewsletter/68 and when i click url button then it gives wrong url http://devweb.tsgdomain.com/nphnewdemo/Enewsletter/popup.aspx?name=dev-test-please-review-it insted of this url http://devweb.tsgdomain.com/nphnewdemo/popup.aspx?name=dev-test-please-review-it.
Please suggest some answers to why this is happening (and how I can make this work correctly).

This is happens because the form is rendering using the actual URL (and not what user see).
If you like to change that you can be rewriting the form attribute making a global handler of the form.
Here is a full solution that is tested and working.
http://www.koders.com/csharp/fid39B3A4A2AD871AA78E7E5D8643A076EF4352CDF9.aspx
In the aboce code the line that make the change is the
value = Context.Request.RawUrl;

Related

Strangely Query String is disappeared on live server

Maybe this is a very normal behaviour or I am missing something very basic. I have a page named Default.aspx and it is set to Default page for my web application. On this page there is a dropdownlist for categories and when I select any value then response.redirect is called for the same page and category id is passed as a query string. Now everything work just fine when I test the application locally. When I publish the website on the server and call the website with url www.mywebsite.com then the default page is displayed but page name is not displayed. I have no issue with it but without the page name my response.redirect stops working. So just to summarize all this in following lines,
www.mysite.com
// On Selected Index Changed
Response.Redirect("~/Default.aspx?catId="+ ddlCategory.SelectedItem.Value);
The Url should be for first category
www.mysite.com/Default.aspx?catId=1
but it is not changing and on postback www.mysite.com is displayed.
However if in the address bar I have,
www.mysite.com/Default.aspx
and now if I change the selected index of drop down then it works fine and the url becomes,
www.mysite.com/Default.aspx?catId=1
Now I don't know whether this question was asked already because I don't even know what the issue is and what should I search.
Update:
Now this is reproduced in the local system also. It seems that when the page name Default.aspx is not present in the address bar with url then SelectedIndexChanged is not called. If Default.aspx is in the address bar then SelectedIndexChanged is fired.
Well I am still unable to find the exact reason for the behaviour that I have mentioned in my question but I have applied a workaround. I am now not calling the SelectedIndexChange event from code behind, instead I am using Javascript to redirect to the same page with query string.
<script type="text/javascript">
function CategoryChanged() {
var ddlReport = document.getElementById("<%=ddlCategory.ClientID%>");
window.location = 'Default.aspx?catId=' + ddlCategory.value;
}
</script>
And called this on onchange of my dropdownlist,
onchange="CategoryChanged()"
Beside this solution I would love to know that why the code behind event was not called when page name is not mentioned in the url. So if anyone has any idea do share their thoughts.

ASP.NET Redirection

I'm currently redirecting a page using
Response.Redirect("URL");
this works great, however it changes the url too. For example:
http://localhost/index.aspx
Redirected to: test.aspx will change the url to:
http://localhost/test.aspx
Is it possbile to redirect the content without changing the URL?... so the Url will be:
http://localhost/test.aspx
BUT the content would of been redirected to
test.aspx
Thank you.
Try to use Server.Transfer(URL); to avoid changing the url.
More info here.
And also check the differences at this post and this one.
Make sure that this solution meets your needs.
Use Server.Transfer
This will show the content of default.aspx page, but it will not change the url
Server.Transfer("Default.aspx");

ASP.NET Postback and URLs

I have a page that requires the user to go through several steps, however step is performed on the same ASPX page with different panels being displayed.
However this is a requirement that each step has a different URL, this could be a simple as a query string parameter, for example:
Step 1:
/member/signup.aspx?step=1
Step 2:
/member/signup.aspx?step=2
Step 3:
/member/signup.aspx?step=3
However I don't want to have to redirect the user to the new URL each time they continue to the next step, this would involve a lot of redirecting and also a switch statement on the page load to work out which step the user is on.
It would be better if I could alter the URL that is displayed to the user when the original request is sent back to the user, i.e. the user click "next" on step 1 the page then does some processing and then alters response so that the user then sees the step 2 URL but without any redirection.
Any ideas would be greatly appreciated.
Could you convert your Panels into steps in a Wizard control?
It would be a little more complicated than you probably want, but you could achieve this effect with the PostBackUrl property of the submitting button. I'm assuming each panel has its own "submit" button, and they could all use this property to "advance" the process. The drawback is that in order to get to submitted controls, you'd need to use the Page.PreviousPage property in order to access any controls and their values.
You could programmatically alter the PostbackUrl property of your 'Next' button on each Page_Load, based on the query string value. This is a bit strange though, as you wouldn't be able to use a standard event handler for the button click, and you'd have to use the PreviousPage property of the Page to get the data from the previous tab.
I'd say challenge this requirement. Why would anyone need to jump into the middle step? If it's a case of displaying the progress to the user, do this on the page, not in the URL.
You require that each step has different URL, than Response.Redirect is the only option. As you want to avoid the redirection, you can use IFrame but IFrame URL is not visible to user on his browser. I think redirect option is ugly(for both SERVER and CLIENT) as in this case, you first post on the page and than get that page. The best solution is POST BACK with some varible tracking step.
You could implement a form or url rewriting so that your urls end up being
/member/signup/step1/
/member/signup/step2/
/member/signup/step3/
To do this use the
HttpContext.RewritePath method which means you can rewrite /member/signup/step1/ to /member/signup.aspx?step=1 for example. See an example here.
I would also use the PRG (post request get) pattern so that each next link posts the form data of that step to the session then redirects the user top the correct next url. this will ensure that the user can navigate back and forth through the steps safely and also the url will remain intact in all your posts.
Check out server.transfer

How to remove query string parameters from the url?

Assume I have the link http://www.somesite.com/file.aspx?a=1&b=2
And now I want to remove all the parameters, so it becomes:
http://www.somesite.com/file.aspx
Or I may want to remove only 1 of the parameters such as
http://www.somesite.com/file.aspx?b=2
Is there a way to do the above in C#? What is happening is that I am coming from another page with a parameter called edit in the url, but when the page does a post back, the edit parameter is still there, so it still thinks it is in edit mode. Example:
User A goes to page one.aspx and clicks on an edit link. They are taken to two.aspx?edit=true. During page load, it sees the the query string parameter edit is not null and it puts the contents in edit mode. Once the user is done editing, the page is refreshed, but the url is still two.aspx?edit=true and keeps the contents in edit mode, when in fact it should be two.aspx
Request.Querystring is read-only collection - You cannot modify that.
If you need to remove or change the param in querystring only way out is to trigger a new GET request with updated querystring - This means you will have to do Response.Redirect with updated URL. This will cause you lose the viewstate of the current page.
Use the PostBackUrl property, for example:
<asp:Button ID="DoneEditingButton" runat="server" Text="Done editing" PostBackUrl="~/two.aspx" />
When you are done with the edit you are doing a post back so just define the action to post to two.aspx instead of just posting back to itself that way it will drop off the get parameters.
How about checking Page.IsPostBack to see if the current request is a postback or not?
if you have only string, you can use:
strinULR.Split('?').First();
or
strinULR.Split('?').FirstOrDefault();
Try something like this.
if (url.Contains("?"))
url = url.Substring(0, url.IndexOf("?"));
In this example, I'm checking if the url even contains a query string, and if it does, subtracting getting the "left part" of the string prior to the ?.
Late but you can do this to remove query string from URL without another GET Request.
http://www.codeproject.com/Tips/177679/Removing-Deleting-Querystring-in-ASP-NET

Getting url of a page once making a form submission

I am designing a winforms based testing app (which is based upon WatiN). I specify a page to test and the value to insert into a textbox, and then click the corresponding button.
Is it possible to add a query string to the request I make (when clicking button) and then get the URL of the next page? Based on this, I need to screen scrape it.
Not sure about Watin syntax, but in Watir (Ruby version) you can get URL of the page displayed in browser with
browser.url
Or do you need to get URL of the next page before you open it?
Based on your comment to AmitK, Ċ½eljko's answer is right.
In WatiN (and C#), the syntax is:
Console.WriteLine("The current page is:" + ie.Url.ToString());
(just in case: 'ie' is the browser reference, use whatever object you use to enter text and click buttons)
What exactly do you mean by "next page" ? Does the form when submitted redirect to another page? If so, you will receive a HTTP 302/303 status code with the URL of the next page.

Categories