WebApi 2.0路由问题:可进入控制器构造函数但无法触发Action
Let's break down why your MyMethodName action isn't firing even though you can hit the MyTestController constructor, and walk through the fixes step by step:
1. Verify You're Using the Correct HTTP Verb
Your action is decorated with [HttpPost], which means only POST requests will trigger it. If you're testing with a GET request (like typing the URL directly in a browser), that's the immediate issue.
To test properly:
- Use tools like Postman, curl, or Thunder Client.
- Set the request method to
POST. - Add the
Content-Type: application/jsonheader (critical for binding the[FromBody]model).
Example curl command:
curl -X POST -H "Content-Type: application/json" -d '{"Field1": "1", "Field2": "some key", "Field3": "some name of cour..."}' http://localhost:64704/api/mytest/mymethod
2. Check Attribute Route Configuration
Your route setup looks correct on the surface:
- Controller-level
[Route("api/[controller]")]combined with action-level[Route("mymethod")]creates the valid routeapi/MyTest/mymethod(matches your request URL). - You're calling
config.MapHttpAttributeRoutes();before registering the default route, which is the correct order for attribute routing to take precedence.
No changes needed here, but double-check that no other controllers or routes are conflicting with this path.
3. Fix Model Binding Issues
If the JSON payload can't bind to your MyModel class, WebApi will silently skip invoking the action. Here's what to check:
- Match JSON field names to model properties: Ensure your
MyModelclass has public properties namedField1,Field2,Field3(case-sensitive by default). If your model uses camelCase properties (e.g.,field1), use the[JsonProperty]attribute to map them:public class MyModel { [JsonProperty("Field1")] public string Field1 { get; set; } [JsonProperty("Field2")] public string Field2 { get; set; } [JsonProperty("Field3")] public string Field3 { get; set; } } - Validate JSON syntax: Your provided JSON looks valid, but ensure there are no trailing commas or typos in the actual request.
4. Confirm Unity Dependency Registration
While the controller constructor runs (so Unity is resolving IMyFacade), double-check that you've registered the concrete implementation of IMyFacade in your UnityConfig:
public static void RegisterComponents() { var container = new UnityContainer(); // Add this line (replace MyFacade with your actual implementation) container.RegisterType<IMyFacade, MyFacade>(); GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container); }
Missing this registration could cause unexpected behavior later, even if the constructor runs (though it's not the direct cause of the action not firing here).
5. Enable Tracing to Debug Route Matching
To get deeper visibility into why the action isn't being matched, enable WebApi tracing:
- Install the
Microsoft.AspNet.WebApi.TracingNuGet package. - Add this line to your
WebApiConfig.Registermethod:config.EnableSystemDiagnosticsTracing(); - Check the Output window in Visual Studio for detailed logs about route matching, model binding, and any errors that occur before the action is invoked.
内容的提问来源于stack exchange,提问作者TheBoubou

