You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mvc中使用RedirectToAction无法打开新.cshtml页面的问题求助

Troubleshooting RedirectToAction Not Updating URL

Hey there, let's figure out why your RedirectToAction isn't changing the browser URL as expected, even though the Depot ActionFilter is executing. Here are the most likely causes and fixes:

1. You're using an AJAX POST request (the most common culprit)

If you're sending the POST request via AJAX (like with jQuery's $.post, axios, or fetch), the browser won't automatically follow the 302 redirect returned by RedirectToAction. Instead, the AJAX call will receive the redirect response, but the address bar stays the same, and the new View content is just returned as data to the AJAX handler (not rendered on the page).

Fix for AJAX requests:

Modify your backend action to return a JSON response with the redirect URL, then handle the redirect on the frontend:

Backend code update:

[HttpPost] 
public ActionResult Index(string storageid) 
{ 
    // Check if the request is coming from AJAX
    if (Request.IsAjaxRequest())
    {
        // Generate the redirect URL
        string redirectUrl = Url.Action("Depot", "Depot", new { storageid = storageid });
        // Return JSON with the URL to redirect to
        return Json(new { RedirectUrl = redirectUrl });
    }
    
    // For non-AJAX requests, perform normal redirect
    return RedirectToAction("Depot", "Depot", new { storageid = storageid }); 
}

public ActionResult Depot(string storageid) 
{ 
    return View("Depot"); 
}

Frontend AJAX handler update:

// Example using jQuery
$.post('/YourControllerName/Index', { storageid: 'your-storage-id' }, function(response) {
    if (response.RedirectUrl) {
        // Force the browser to navigate to the new URL
        window.location.href = response.RedirectUrl;
    }
});

2. Parameter name mismatch (minor but worth checking)

In your original code, you're passing storageId (capital D) in the route values, but your Depot action expects storageid (lowercase d). While ASP.NET MVC's model binding is case-insensitive, keeping parameter names consistent avoids unexpected issues. Update your redirect line to match the action's parameter name:

return RedirectToAction("Depot", "Depot", new { storageid = storageid });

3. Switch to a regular form submission (if AJAX isn't necessary)

If you don't need AJAX for this request, use a standard HTML form to submit the data. This will let the browser handle the redirect naturally, updating the URL as expected:

<form action="/YourControllerName/Index" method="post">
    <input type="hidden" name="storageid" value="your-storage-id" />
    <button type="submit">Go to Depot</button>
</form>

Why the Depot ActionFilter is executing but URL doesn't change

If your AJAX call is receiving the View content from the Depot action directly (without a redirect), that means the RedirectToAction might not be triggering at all, or the AJAX handler is bypassing the redirect. Double-check the Network tab in your browser's dev tools to see the sequence of requests:

  • Look for the initial POST to /Index
  • Check if it returns a 302 status code pointing to /Depot
  • If no subsequent GET request to /Depot is made, AJAX is definitely the issue

内容的提问来源于stack exchange,提问作者Android Dvlpr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:00:26