简体   繁体   English

如何在同一控制器中获得MVC4 Web API以支持HTTP动词和“操作”方法?

[英]How do I get MVC4 Web API to support HTTP Verbs and “Action” methods in the same controller?

How do I setup routing to support this? 如何设置路由以支持此功能?

  • GET /api/values/ works GET / api / values / 有效
  • GET /api/values/1 works GET / api / values / 1 有效
  • POST /api/values works POST / api / values 有效
  • PUT /api/values works PUT / api / values 有效
  • DELETE /api/values works 删除/ api / values 有效
  • GET /api/values/GetSomeStuff/1 DOES NOT WORK! GET / api / values / GetSomeStuff / 1不起作用

If I switch the routes around, then GetSomeStuff works, but then /api/values does not work. 如果我切换路由,则GetSomeStuff可以工作,但是/ api / values不能工作。 How do I configure route to have them both work? 如何配置路由以使它们都起作用?

Example methods: 示例方法:

 // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }

    // GET api/values/5
    [HttpGet]
    public string GetSomeStuff(int id)
    {
        return "stuff";
    }

Routes are setup like this: 路由设置如下:

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

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

How about instead of: 而不是:

GET /api/values/GetSomeStuff/1

you designed your urls like that: 您是这样设计网址的:

GET /api/someStuff/1

Now you could simply have a SomeStuffController and a Get(int id) action on it. 现在,您可以简单地对其进行SomeStuffControllerGet(int id)操作。

You could try the following routes: 您可以尝试以下路线:

        config.Routes.MapHttpRoute(
            name: "DefaultController",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^[0-9]*$" }
        );

        config.Routes.MapHttpRoute(
            name: "DefaultController2",
            routeTemplate: "api/{controller}/{action}/{id2}"
        );

and change your action method to: 并将您的操作方法更改为:

    [HttpGet]
    public string GetSomeStuff(int id2)
    {
        return "stuff";
    }

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

相关问题 我如何获取MVC4 Url.Action方法在控制器之前包含哈希 - How do I get MVC4 Url.Action method to include a hash before the controller Web API:具有不同HTTP谓词的相同方法 - Web API: Same Method with different HTTP Verbs 给定一个URL和HTTP动词,如何在ASP.NET MVC / Web API中解析控制器/动作? - Given a URL and HTTP verb, how can I resolve the controller/action in ASP.NET MVC/Web API? 如何使用MVC4 Web API控制器调用两个以上的Get方法 - How to give a call to more than two Get method using mvc4 web api controller 如何编写REST服务以支持.net MVC Web API中的多个Get Endpoints? - How do I write REST service to support multiple Get Endpoints in .net MVC Web API? MVC4区域路由:默认控制器上的操作方法 - MVC4 Area route: action methods on default controller .NET MVC4-在同一API控制器操作中从模型返回不同的字段子集 - .NET MVC4 - Return different subset of fields from Model in the same API controller action 测试MVC控制器动作HttpAcceptAttribute动词 - Testing MVC Controller Action HttpAcceptAttribute Verbs 如何像Web API 2一样声明MVC 5操作? - How do I declare an MVC 5 action like in Web API 2? 如何使用MVC4在Controller中获得主机 - How can I get host in a Controller using MVC4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM