2009-05-02 41 views
95

讓我們假設我有一些網頁如何重定向到ASP.NET MVC中的上一個操作?

  • some.web/articles/details/5
  • some.web/users/info/bob
  • some.web/foo/bar/7

,可以撥打的共同效用控制器像

locale/change/esauthorization/login

如何獲得這些方法(changelogin)重定向到以前的行爲(detailsinfobar),而通過前面的參數給他們(5bob7)?

簡而言之:如何在另一個控制器中執行操作後,如何重定向到剛訪問的頁面?

回答

121

嘗試:

public ActionResult MyNextAction() 
{ 
    return Redirect(Request.UrlReferrer.ToString()); 
} 

另外,觸及什麼達林說,試試這個:

public ActionResult MyFirstAction() 
{ 
    return RedirectToAction("MyNextAction", 
     new { r = Request.Url.ToString() }); 
} 

則:

public ActionResult MyNextAction() 
{ 
    return Redirect(Request.QueryString["r"]); 
} 
+65

關閉。我用 return Redirect(Request.UrlReferrer.ToString()); – adolfojp 2009-05-03 01:44:34

6

傳遞一個RETURNURL參數(URL編碼)的變化登錄行動和內部重定向到這個給定的RETURNURL。您的登錄操作可能會是這個樣子:

public ActionResult Login(string returnUrl) 
{ 
    // Do something... 
    return Redirect(returnUrl); 
} 
7

建議如何做到這一點UCH的是:

  1. 返回URL生存
  2. 返回URL從初始的推薦網址
  3. 確定不使用的TempData []或其他服務器端狀態的形式的POST請求(和任何校驗失敗)
  4. 處理(通過提供一個默認的重定向)

直接導航到行動。

public ActionResult Create(string returnUrl) 
{ 
    // If no return url supplied, use referrer url. 
    // Protect against endless loop by checking for empty referrer. 
    if (String.IsNullOrEmpty(returnUrl) 
     && Request.UrlReferrer != null 
     && Request.UrlReferrer.ToString().Length > 0) 
    { 
     return RedirectToAction("Create", 
      new { returnUrl = Request.UrlReferrer.ToString() }); 
    } 

    // Do stuff... 
    MyEntity entity = GetNewEntity(); 

    return View(entity); 
} 

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Create(MyEntity entity, string returnUrl) 
{ 
    try 
    { 
     // TODO: add create logic here 

     // If redirect supplied, then do it, otherwise use a default 
     if (!String.IsNullOrEmpty(returnUrl)) 
      return Redirect(returnUrl); 
     else 
      return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); // Reshow this view, with errors 
    } 
} 

您可以使用重定向這樣的觀點中:

<% if (!String.IsNullOrEmpty(Request.QueryString["returnUrl"])) %> 
<% { %> 
    <a href="<%= Request.QueryString["returnUrl"] %>">Return</a> 
<% } %> 
25

如果你不與單元測試有關,那麼你可以簡單的寫:

return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString()); 
33

如果你想從視圖中的按鈕重定向您可以使用:

@Html.ActionLink("Back to previous page", null, null, null, new { href = Request.UrlReferrer}) 
6

在MVC中使用純HTML 在查看頁面與Java腳本onclick

<input type="button" value="GO BACK" class="btn btn-primary" 
onclick="location.href='@Request.UrlReferrer'" /> 

這個偉大的工程。希望能幫助某人。

@JuanPieterse已使用已經回答@Html.ActionLink所以如果可能有人可以發表評論或使用@Url.Action

1

您可以通過使用ViewBag.ReturnUrl屬性返回到前一個頁面回答。

1

動態構造的RETURNURL任何視圖,試試這個:

@{ 
    var formCollection = 
     new FormCollection 
      { 
       new FormCollection(Request.Form), 
       new FormCollection(Request.QueryString) 
      }; 

    var parameters = new RouteValueDictionary(); 

    formCollection.AllKeys 
     .Select(k => new KeyValuePair<string, string>(k, formCollection[k])).ToList() 
     .ForEach(p => parameters.Add(p.Key, p.Value)); 
} 

<!-- Option #1 --> 
@Html.ActionLink("Option #1", "Action", "Controller", parameters, null) 

<!-- Option #2 --> 
<a href="/Controller/Action/@[email protected](ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), parameters)">Option #2</a> 

<!-- Option #3 --> 
<a href="@Url.Action("Action", "Controller", new { object.ID, returnUrl = Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), parameters) }, null)">Option #3</a> 

這也適用於佈局頁,局部視圖和HTML輔助

相關:MVC3 Dynamic Return URL(相同的,但是從在任何控制器/行動內)

1

我使用.Net Core 2 MVC,這一個爲我工作, 在控制r使用 HttpContext.Request.Headers["Referer"];

相關問題