views:

168

answers:

2

Hello All:

I have one application in which I want to pass data between Pages (Views) without sessions. Actually I want to apply some settings to all the pages using query string. For example if my link is like "http://example.com?data=test1", then I want to append this query string to all the link there after and if there is no query string then normal flow.

I was thinking if there is any way that if we get the query string in any link for the web application then some application level user specific property can be set which can be used for subsequent pages.

Thanks, Ashwani

+1  A: 

You can get the query string using the

Request.Url.Query

and on your links to the other page you can send it.

Here is an idea of how you can find and change your page:

public abstract class BasePage : System.Web.UI.Page
{
    protected override void Render(System.Web.UI.HtmlTextWriter writer)    
    {
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();

        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

        // now you render the page on this buffer
        base.Render(htmlWriter);

        // get the buffer on a string
        string html = stringWriter.ToString();

        // manipulate your string html, and search all your links (hope full find only the links)
        // this is a simple example of replace, THAT PROBABLY not work and need fix         
        html = html.Replace(".aspx", ".aspx?" + Request.Url.Query);

        writer.Write(html);
    }
}

I do not suggest it how ever, and I think that you must find some other way to avoid to manipulate all your links...

Aristos
Thanks. But the problem is that there can be many link on a page and I want to pass the query string value to all the pages. Is there any way to set some kind of property which can be accessed across pages?
Ashwani K
@Ahwani I can think some ways... hmm maybe if you get the final render of the page and change all the links... I do not like the idea.. but...
Aristos
Yea, I thought of this too. But the problem here is that links are generated dynamically. Can't we have some property kind of thing which can be shared across views?
Ashwani K
A: 

I don't undestand what kind of data are you trying to pass. Because it sounds weird to me the idea of trapping all links.

Anyway, I believe you may find the class TempData usefull for passing data between redirects.

And a final warning, be carefull about TempData, it has changed a little between MVC 1 and 2:
ASPNET MVC2: TempData Now Persists

Protron
Actually, I have some settings which can be set using query string like changing css files. This work for the page which is called with query string, but if some body navigates to other links from that page, I want that value to be available.
Ashwani K