网页API找不到动作时,可选参数没有美元的URL p $ psent [英] Web API action not found when optional parameter not present in URL

查看:157
本文介绍了网页API找不到动作时,可选参数没有美元的URL p $ psent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙。
我不得不改变路由的Web API,以便能够使用URL方式:

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 }
        );

    }
}

我有一个控制器

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

一切工作正常与参数的方法,但我不能叫参像 http方法://本地主机/ API /位置/批准。而不是调用批准方法我得到了404没有发现错误。我做错了什么?

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?

有趣的部分:调用网址的http://本地主机/ API /位置/核准/不管的作品。好像ID不是那么可选的,因为我想。

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

感谢您的帮助!

推荐答案

您的问题是,第一条路线(MVC)正在匹配,而不是你真正想要的路线。

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

因此​​,对于像一个网址的http://本地主机/ API /位置/批准应用程序正在寻找一个名为APIController与所谓的动作控制器'位置'有一个字符串参数'身份证',这将被设置为认可的值。

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".

快速的解决办法是让API路线的MVC路线之前出现,但是作为pviously提到我的路线分成各自CONFIGS(RouteConfig&安培; WebApiConfig)$ P $更改路线的声明,并保证在在Global.asax.cs中路由注册在正确的顺序:

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);
}

另外,如果您使用的是WebApi2你可以使用属性来路由使事情变得更容易。

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

这篇关于网页API找不到动作时,可选参数没有美元的URL p $ psent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆