0

Assuming that I'm talking about an ASP.NET MVC 3 application, the scenario is something like

  1. I browse to the URL http://localhost:60088/Example?param1=test123 that returns a view
  2. Next I click a button that triggers an Ajax call to the URL http://localhost:60088/Example/DoSomethign that 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:

  1. Is safe to assume that this.Request.UrlReferrer will always have the URL that was called in a non-callback way (even if I made several more callbaks after the _DoSomething_ first callback)?

  2. Is there a better way to accomplish waht I'm trying to do?

4

2 回答 2

3

不; 您不能假设引荐来源网址将始终存在。

相反,您应该将原始 URL 作为参数包含在 AJAX 请求中。

于 2012-09-19T23:36:51.130 回答
0

在返回视图的操作中,您可以将 a 添加param1到 aViewBag中,如下所示:

public ActionResult Example(string param1)
{
    ViewBag.Param1 = param1;
    return View();
}

然后在 ajax 调用中,只需传递ViewBag.Param1作为路由值。它有时可能是空的,但这不应该伤害任何东西。

于 2012-09-20T02:39:13.683 回答