简体   繁体   English

无法使用MVC5使用控制器名称和操作名称访问WebAPI

[英]Unable to access WebAPI using Controller and Action name using MVC5

I have an WebAPI, 我有一个WebAPI,

 public class SystemHealtController : ApiController
    {
        [HttpGet]
        public IHttpActionResult UpdateUsageDetail(string cpuUsage, string memoryUsage, string diskUsage)
        {

              // Do Stuff

            return Ok();
        }

RouteConfig also have default settings, RouteConfig也具有默认设置,

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

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

But when I navigate to API URL, http://localhost:64384/API/SystemHealt/UpdateUsageDetail?cpuUsage=2&memoryUsage=4&diskUsage=76 , get following error: 但是当我导航到API URL http://localhost:64384/API/SystemHealt/UpdateUsageDetail?cpuUsage=2&memoryUsage=4&diskUsage=76 ,出现以下错误:

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. HTTP404。您正在寻找的资源(或其依赖项之一)可能已被删除,名称更改或暂时不可用。 Please review the following URL and make sure that it is spelled correctly. 请查看以下网址,并确保其拼写正确。

The only thing I suspect is, before creating Controller, I Installed 我唯一怀疑的是,在创建Controller之前,我已经安装了

Microsoft.AspNet.WebApi 

and then uninstalled as well suspecting that could be the issue. 然后卸载,并怀疑这可能是问题所在。 But after uninstall also there were no errors. 但是卸载后也没有错误。

By default, the web api routing is registered in the WebApiConfig class with the route pattern api/{controller}/{id} 默认情况下,Web api路由使用路由模式api/{controller}/{id}WebApiConfig类中注册。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

You can see that there is no action method name. 您会看到没有操作方法名称。 the methods are accessed by the verb (POST or GET, the type of call you are making). 可以通过动词(POST或GET,您正在拨打的电话的类型)访问这些方法。 So typically you will create GET methods (to read data) and a POST method to post data to (for creating /updating data). 因此,通常您将创建GET方法(以读取数据)和POST方法以将数据发布到(用于创建/更新数据)。 When you want to use these endpoints, you will use the same url, but the Http method will be different (GET and POST) 当您要使用这些端点时,将使用相同的url,但是Http方法将有所不同(GET和POST)

So you should be accessing it like 所以你应该像这样访问它

yourSiteBaseUrl/api/SystemHealt?cpuUsage=2&memoryUsage=4&diskUsage=76

This should work, assuming you have Web api enabled in the app. 假设您在应用程序中启用了Web api,这应该可以工作。 If you are manually adding the web api functionality to an existing mvc project, Follow the steps mentioned in this post to do so. 如果手动添加Web API功能到现有的MVC项目,按照中提到的步骤这个帖子这样做。

By default, the route registration in RouteConfig is used for MVC controllers. 默认情况下, RouteConfig的路由注册用于MVC控制器。

have you tried putting a route prefix? 您是否尝试放置路由前缀?

[RoutePrefix("api/SystemHealt")]
public class SystemHealtController : ApiController
{
    [HttpGet]
    public IHttpActionResult UpdateUsageDetail(string cpuUsage, string 
     memoryUsage, string diskUsage)
    {

          // Do Stuff

        return Ok();
    }

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

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