ASP.NET MVC返回JSON同时跳转视图失败的问题求助
Hey there! Let's break down why your current code isn't working, then walk through practical solutions to handle this scenario.
Why Your Current Code Fails
First, let's look at your code snippet:
public ActionResult MBBDirectResponse(MBBModel model) { RedirectToAction("Index", "CorporateExercise", new { area = "Applicant" }); return Json(mbb); }
The issues are straightforward:
- You call
RedirectToAction()but don't return its result—this method creates aRedirectResultobject, but since you ignore it, it gets discarded immediately. - Most importantly, HTTP only allows one response per request. You can't send both a JSON payload and a redirect instruction at the same time. The third-party caller will only receive the JSON response, so the redirect never fires.
Solutions to Handle Both JSON and Redirect
Since your third-party needs JSON data and you need a redirect, here are two reliable approaches:
Option 1: Return JSON with a Redirect URL (Recommended)
The cleanest, most standards-compliant way is to include the redirect URL directly in your JSON response. Let the caller (frontend or third-party service) handle the redirect once they receive the data.
Here's the updated code:
public ActionResult MBBDirectResponse(MBBModel model) { // Combine your original JSON data with the redirect URL var responsePayload = new { MbbData = mbb, // Your existing MBB model data RedirectUrl = Url.Action("Index", "CorporateExercise", new { area = "Applicant" }) }; // Return the combined JSON response return Json(responsePayload, JsonRequestBehavior.AllowGet); }
Once the third-party receives this JSON, they can extract the RedirectUrl value and trigger the redirect (for example, using window.location.href = RedirectUrl if it's a browser-based caller).
Option 2: Return an HTML Page with Auto-Redirect and JSON Data
If the third-party can't parse the redirect URL from JSON, you can return an HTML page that embeds your JSON data and a script to auto-redirect. This works by packing both logic into a single HTML response:
using Newtonsoft.Json; // Ensure Newtonsoft.Json is installed in your project public ActionResult MBBDirectResponse(MBBModel model) { // Serialize your MBB data to a safe JSON string string jsonMbbData = JsonConvert.SerializeObject(mbb); jsonMbbData = jsonMbbData.Replace("'", "\\'"); // Escape quotes to avoid script errors // Build an HTML page that stores the JSON and triggers a redirect string htmlResponse = $@" <script> // Store the JSON data (e.g., in localStorage for later use) localStorage.setItem('mbbResponseData', '{jsonMbbData}'); // Auto-redirect to the target view window.location.href = '{Url.Action("Index", "CorporateExercise", new { area = "Applicant" })}'; </script> "; // Return the HTML content return Content(htmlResponse, "text/html"); }
Note: This approach depends on the caller allowing embedded JavaScript execution, so it's only suitable if the third-party expects HTML responses.
Final Takeaway
Always remember: HTTP is a request-response protocol—one request can only have one response. Option 1 is almost always the best choice because it keeps your API compliant and flexible for different types of callers.
内容的提问来源于stack exchange,提问作者izzwan

