Revel框架中含URL的路由参数无法匹配控制器的问题解决问询
Got it, let's tackle this problem head-on. The core issue here is that Revel's default route matching splits paths by slashes—even if you encode the URL parameter, servers/browsers often auto-decode those slashes before the route matcher sees them, turning your two-param route into multiple segments and causing the 404.
Here are the steps to get this working properly:
1. Switch to a Catch-All Route
Revel supports catch-all wildcards (*) that capture everything after a certain point in the path. Modify your route definition in the routes file to use this for the value parameter:
GET /properties/configurations/:name/*value App.PropertiesConfig
The *value will grab every character (including encoded/decoded slashes) after the :name segment and assign it to the value parameter.
2. Update Your Controller to Handle the Parameter
In your Go controller method, you'll receive the encoded value string—you just need to decode it to get the original URL. Here's an example implementation:
import "net/url" func (c App) PropertiesConfig(name, value string) revel.Result { // Decode the URL-encoded value to get the original URL decodedValue, err := url.QueryUnescape(value) if err != nil { return c.BadRequest("Invalid parameter encoding: " + err.Error()) } // Your business logic here—use name and decodedValue as needed return c.RenderJSON(map[string]string{ "name": name, "original_value": decodedValue, "encoded_value": value, }) }
3. Ensure Frontend Encoding is Correct
Keep using encodeURIComponent() on the full value parameter in your JavaScript—this ensures special characters (like slashes, colons) are properly escaped before the request is sent. Example:
const configName = "app.url"; const configValue = "http://test.test"; const encodedValue = encodeURIComponent(configValue); const requestUrl = `/properties/configurations/${configName}/${encodedValue}`; // Fetch example fetch(requestUrl) .then(response => response.json()) .then(data => console.log(data)) .catch(err => console.error(err));
After encoding, http://test.test becomes http%3A%2F%2Ftest.test—the catch-all route will capture this entire string as the value parameter.
4. Test with Curl to Validate
Before relying on frontend code, test the route directly with curl to confirm it works:
curl "http://your-revel-app.com/properties/configurations/app.url/http%3A%2F%2Ftest.test"
You should get a valid response from your controller instead of a 404.
Key Notes
- The catch-all wildcard (
*) must be the last segment in your route—Revel doesn't support wildcards in the middle of a path. - If your
valueincludes anchors (#), browsers won't send them to the server—you'll need to handle those separately if they're required (though this isn't an issue for standard URLs like your example). - Double-check that your Revel version supports catch-all routes (all recent versions do, but if you're on a very old release, you may need to upgrade).
内容的提问来源于stack exchange,提问作者Stanislav Bozhanov

