如何在视图与控制器间传递上一页/调用页/来源URL?
Hey there! Let's work through how to make that return button behave reliably for your Course and Pages controllers. You mentioned using Request.UrlReferrer.ToString()—that works in simple cases, but it can be unreliable if browsers block referrer headers or users navigate directly to the Pages views. Here are a few solid approaches to handle this:
Request.UrlReferrer Approach (Quick Fix) If you want to stick with the referrer method, add a fallback for when the referrer is missing, and pass it to your Pages view properly:
- In your Pages controller actions (Edit/Details), grab the referrer and set a default if it's null:
public IActionResult Edit(int pageId) { // Fetch your page data here... // Set return URL: use referrer if available, else fall back to a safe default ViewBag.ReturnUrl = Request.UrlReferrer?.ToString() ?? Url.Action("Index", "Course"); return View(); }
- In your Pages view, link the "返回" button to this URL:
<a href="@ViewBag.ReturnUrl" class="btn btn-secondary">返回</a>
Note: This still depends on the browser sending the referrer header, which isn't guaranteed (some privacy settings block it).
returnUrl Parameter (Most Reliable) For full control, pass the original Course view URL as a parameter when linking to Pages. This avoids relying on browser behavior entirely:
- In your Course Edit/Details views, when generating links to Pages actions, include the current URL as
returnUrl:
<!-- Example: Link to edit a page from Course/Edit view --> <a href="@Url.Action("Edit", "Pages", new { pageId = page.Id, returnUrl = Request.Path.ToString() })" class="btn btn-primary">编辑页面</a> <!-- Same pattern for Details links --> <a href="@Url.Action("Details", "Pages", new { pageId = page.Id, returnUrl = Request.Path.ToString() })" class="btn btn-info">查看页面详情</a>
- In your Pages controller, accept the
returnUrlparameter, validate it (to prevent open redirect attacks), and pass it to the view:
public IActionResult Edit(int pageId, string returnUrl) { // Validate the return URL is local to your app to avoid security risks if (!Url.IsLocalUrl(returnUrl)) { returnUrl = Url.Action("Index", "Course"); } ViewBag.ReturnUrl = returnUrl; // Fetch your page data here... return View(); }
- Use the same view code as above for the return button—
@ViewBag.ReturnUrlwill now point back to the exact Course view that linked to the Pages page.
TempData for Post-Redirect Scenarios If you're handling POST requests (e.g., saving a page and redirecting back to the Pages view), use TempData to persist the return URL across redirects:
- In your Pages POST action:
[HttpPost] public IActionResult Edit(PageViewModel model, string returnUrl) { if (ModelState.IsValid) { // Save your page changes here... // Store the return URL in TempData to keep it after redirect TempData["ReturnUrl"] = returnUrl; return RedirectToAction("Edit", "Pages", new { pageId = model.Id }); } // If validation fails, keep using the original return URL ViewBag.ReturnUrl = returnUrl; return View(model); }
- In your Pages GET action, retrieve the URL from
TempDataif available:
public IActionResult Edit(int pageId) { string returnUrl = TempData["ReturnUrl"]?.ToString() ?? Url.Action("Index", "Course"); if (!Url.IsLocalUrl(returnUrl)) { returnUrl = Url.Action("Index", "Course"); } ViewBag.ReturnUrl = returnUrl; // Fetch page data... return View(); }
My top recommendation is Option 2—it's the most reliable and secure, since you're explicitly controlling the return path instead of relying on browser behavior.
内容的提问来源于stack exchange,提问作者Praveen Rai

