简体   繁体   English

MVC 4 Web API 404

[英]MVC 4 Web Api 404

I have looked all over for some kind of soultion for this and it seems I have it setup correctly and followed all corrections in other questions. 我已经为此寻找了一些灵感,似乎我已经正确设置了它,并在其他问题中进行了所有更正。

When calling " http://localhost/en/api/cart/get " I get: 当调用“ http:// localhost / en / api / cart / get ”时,我得到:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/en/api/cart/get'.","MessageDetail":"No type was found that matches the controller named 'cart'."}

...when trying to access a ApiController setup in an EPiServer CMS/Commerce 7.5+ solution. ...当尝试在EPiServer CMS / Commerce 7.5+解决方案中访问ApiController设置时。

The Controller looks like this: 控制器看起来像这样:

public class CartController : ApiController
{
    [HttpGet]
    public string Get()
    {
        return "OK";
    }
}

In Global.asax.cs i have this: 在Global.asax.cs中,我有以下内容:

protected void Application_Start()
    {
        RegisterApis(GlobalConfiguration.Configuration);

And the RegisterAPis looks like this: RegisterAPis看起来像这样:

public static void RegisterApis(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
         "Api", // Route name 
         "api/{controller}/{action}/{id}", // URL with parameters 
         new { id = RouteParameter.Optional } // Parameter defaults 
        );

        config.Routes.MapHttpRoute(
         "LanguageAwareApi", // Route name 
         "{language}/api/{controller}/{action}/{id}", // URL with parameters 
         new { id = RouteParameter.Optional } // Parameter defaults
        );

        // We only support JSON
        var appXmlType = GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }

On the same machine I have the EPiServer Commerce starterkit running i IIS and the code for registering the api controllers is the same. 在同一台计算机上,我有运行IIS的EPiServer Commerce入门套件,并且用于注册api控制器的代码是相同的。 That site runs fine and the api calls can be made correctly but on my site all I get is 404. 该站点运行良好,可以正确进行api调用,但是在我的站点上,我只能看到404。

So I am probably missing some configuration but I can't for my life figure out what it is. 因此,我可能缺少一些配置,但是我一生都无法弄清它是什么。 The weird part is that on my site I'm running the EPiServer ServiceApi which creates the /episerverapi Web Api mapping and that works just fine. 奇怪的是,在我的网站上,我正在运行EPiServer ServiceApi,该服务创建了/ episerverapi Web Api映射,并且工作正常。

Anyone got any clues on why I can't get my APiControllers to work? 有人知道为什么我的APiControllers无法正常工作吗?

In Web API the http verb help the framework to find the right action to be executed and return a result. 在Web API中,http动词可帮助框架找到要执行的正确操作并返回结果。 For sample, in a case of a get method, you just call the controller by get http verb: 例如,在使用get方法的情况下,您只需通过get http动词来调用控制器:

http://localhost/en/api/cart

It will bind a Get action method in the Cart controller class. 它将在Cart控制器类中绑定Get操作方法。 It is valid for a Post , Put , Delete methods too. 它对PostPutDelete方法也有效。 Keep the default route of asp.net web api 保留asp.net Web API的默认路由

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

Try calling just 尝试打电话给

http://localhost/en/api/cart

In WebAPI if the name of the method matches a HTTP verb then it calls that method when that verb is used on that controller. 在WebAPI中,如果方法的名称与HTTP谓词匹配,则在该控制器上使用该谓词时,它将调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM