.NET Core从SQL Server取数:手动路由跳转首页问题求助
Hey there! Let's work through your problem step by step to get your request info query functionality up and running properly.
First: Fixing the Redirect Issue for Manual Route Access
When accessing /api/requestinfo/details/7507373 gets redirected to the homepage, there are a few common culprits to check:
Mismatched Route Parameter Names
Double-check your controller method's route annotation and parameter binding. For example, if you're using Spring Boot, make sure the@PathVariablename matches exactly what's in your route:// Correct: Path variable name matches the route placeholder @GetMapping("/api/requestinfo/details/{requestId}") public ResponseEntity<RequestInfo> getRequestDetails(@PathVariable("requestId") Long requestId) { // Fetch data from your DAO and return it return ResponseEntity.ok(yourDao.getRequestById(requestId)); }If the names don't match, the framework might fail to resolve the method and fall back to a default route (like your homepage).
Permission/Interception Rules
If you're using a security framework (like Spring Security), verify that your/api/requestinfo/details/**path isn't being intercepted and redirected. Check your security configuration to ensure this endpoint is allowed for unauthenticated users (or whatever your access rules require).Global Exception Handling
If your DAO throws an exception (e.g., request ID not found), check if a global exception handler (@ControllerAdvice) is catching it and redirecting to the homepage. Modify the handler to return an appropriate error response instead of a redirect.Accidental Redirect in Controller
Double-check your controller method's return value. Make sure you're not accidentally returningreturn "redirect:/";instead of returning the fetched data or a proper view.Debug with Browser Dev Tools
Open your browser's Network tab when making the request. Look at the response status code (it'll be a 3xx for redirects) and check theLocationheader to confirm where it's sending you. This can clue you into whether it's a security redirect, a route fallback, or something else.
Second: Implementing Form-Based Request ID Lookup
Once the manual route works, adding a form for your support team is straightforward. Here are two approaches:
Option 1: GET Request with Query Parameter
Create a simple HTML form that submits a GET request with the request ID as a query parameter:
<form action="/api/requestinfo/details" method="GET"> <label for="requestId">请求编号:</label> <input type="text" id="requestId" name="requestId" required> <button type="submit">查询</button> </form>
Then update your controller to accept a query parameter:
@GetMapping("/api/requestinfo/details") public ResponseEntity<RequestInfo> getRequestDetailsByQuery(@RequestParam("requestId") Long requestId) { return ResponseEntity.ok(yourDao.getRequestById(requestId)); }
Option 2: Path Variable with JavaScript Handling
If you want to keep the /api/requestinfo/details/{requestId} route structure, use JavaScript to handle form submission and navigate to the correct path:
<form id="requestQueryForm"> <label for="requestId">请求编号:</label> <input type="text" id="requestId" required> <button type="submit">查询</button> </form> <script> document.getElementById('requestQueryForm').addEventListener('submit', function(e) { e.preventDefault(); // Prevent default form submission const requestId = document.getElementById('requestId').value.trim(); if (requestId) { window.location.href = `/api/requestinfo/details/${requestId}`; } }); </script>
This way, the form submission will redirect to the exact route you already tested (once the redirect issue is fixed).
Final Tips
- Check your backend logs for any error messages related to the request—they'll often tell you exactly why the redirect is happening (e.g., parameter binding errors, security denied exceptions).
- Test the DAO layer directly first to confirm it returns the correct data for the request ID
7507373—this ensures the issue isn't with data retrieval itself.
内容的提问来源于stack exchange,提问作者tjb

