Web API action not found when optional parameter not present in URL

℡╲_俬逩灬. 提交于 2020-01-04 02:48:10

问题


please help. I had to change routing for Web API to be able to use methods in URL:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // for MVC controllers
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Editions", action = "Index", id = UrlParameter.Optional }
        );

        // for Web API
        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new  {  id = RouteParameter.Optional }
        );

    }
}

I have a controller

public class PositionsController : ApiController
{
    [HttpGet]
    public JToken Approved()
    {
        // some code here
    }
}

Everything works fine for methods with parameters, but I cannot call parameterless method like http://localhost/API/Positions/Approved. Instead of calling Approved method I got 404 not found error. What I did wrong?

Funny part: Calling URL http://localhost/API/Positions/Approved/whatever works. It seems like ID is not so optional as I thought.

Thanks for any help!


回答1:


Your problem is that the first route (MVC) is being matched instead of the route you actually want.

So for a url like http://localhost/API/Positions/Approved the app is looking for a controller called 'APIController' with an action called 'Positions' with a string parameter 'id' which will be set to a value of "Approved".

The quick solution is to change the declaration of your routes so the API route appears before the MVC route, however as previously mentioned I would separate the routes into their respective configs (RouteConfig & WebApiConfig) and ensure that in the Global.asax.cs the routes are registered in the correct order:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Alternatively if you are using WebApi2 you could use Attribute Routing to make things easier.



来源:https://stackoverflow.com/questions/23548107/web-api-action-not-found-when-optional-parameter-not-present-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!