开发者

Best practice for URL redirection in ASP.NET web application

开发者 https://www.devze.com 2022-12-18 19:34 出处:网络
I have an asp page (Default.aspx) that displays the diff between two text files. It contains two drop down lists (ID=\"File1\" and \"File2\" respectively) and a button (ID=\"Submit\").

I have an asp page (Default.aspx) that displays the diff between two text files. It contains two drop down lists (ID="File1" and "File2" respectively) and a button (ID="Submit").

It also contains a Literal control with ID "Result".

The contents of the list button are populated based on the files present in the physical application path.

Each time the submit button is clicked, I want to give the result of the diff/comparison in the Literal control, So I have an event handler like this:

protected void Submit_Click(object sender, EventArgs e)
{
    Result.Text = CompareFiles(File1.SelectedValue, File2.SelectedValue);
}

The problem with this is that you cannot bookmark the results of the comparison between specific files.

To solve this problem I appended the values of the files chosen for comparison as query string to the URL and performed a redirection. And therefore, the changed the above event handler to look like this:

protected void Submit_Click(object sender, EventArgs e)
        {
            string build1 = DropDownList1.SelectedValue;
            string build2 = DropDownList2.SelectedValue;

            string queryString = 
                String.Format("?file1={0}&file2={1}",HttpUtility.UrlEncode(build1),HttpUtility.UrlEncode(build2));

            string redirectionUrl = Request.Url.AbsolutePath + queryString;

            Response.Redirect(redirectionUrl, true);
        }

Accordingly called the "CompareFiles" method in the "Page_Load" event handler. So now, you can bookmark 开发者_JS百科the results of the comparison and load it again directly because the URL would contain the files to compare.

But the problem is that "IsPostBack" property is false each time the redirection happens. Therefore, the values of the DropDownLists (files present in the physical application directory) gets evaluated each time.

I do not want this to happen if it is a redirection from the same page. How can I do this? I only want it to happen if the page is accessed for the first time or is accessed from some other page.

Is this a bad practice? I mean redirecting to the same page. I can avoid this problem by giving the results in a different page, But I want to do it this way, because I want the DropDownLists and the "Submit" buttons to always be there.


Since you are redirecting the to same page, it is NOT a postback, viewstate is lost, and it must re-bind the contents.

More than likely, just put the values for the DropDownLists into cache, then you can load from cache, and only hit the file system if not in cache.


Promoted comment to answer:

Request.UrlReferrer
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号