Assuming that I’m talking about an ASP.NET MVC 3 application, the scenario is something like
- I browse to the URL
http://localhost:60088/Example?param1=test123that returns a view - Next I click a button that triggers an Ajax call to the URL
http://localhost:60088/Example/DoSomethignthat returns a JSON string
So, my dilemma is that in the DoSomething controller action I need to get the param1 parameter that was passed in the first step.
My first take is to do something like this:
public ActionResult DoSomething()
{
...
Uri baseUrl = this.Request.UrlReferrer;
// Somehow extract the parameter from baseUrl
...
}
But I’m not sure if that’s a good idea at all…
Questions:
-
Is safe to assume that
this.Request.UrlReferrerwill always have the URL that was called in a non-callback way (even if I made several more callbaks after the_DoSomething_first callback)? -
Is there a better way to accomplish waht I’m trying to do?
No; you cannot assume that the referrer will always exist.
Instead, you should include the original URL as a parameter in the AJAX request.