分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

Why is HttpGet required only for some actions?

发布时间:2023-09-06 02:30责任编辑:蔡小小关键词:暂无标签

https://stackoverflow.com/questions/28068868/why-is-httpget-required-only-for-some-actions

Please refer to the post here

You will see that you can use naming convention (which is why the methods with Get in the name work), or you can explicitly specify the HTTP method for an action by decorating the action with the correct HTTP attribute.

Routing in ASP.NET Web API

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API. This article does not assume any knowledge of ASP.NET MVC.

If you self-host Web API, you must set the routing table directly on the HttpSelfHostConfiguration object. For more information, see Self-Host a Web API.

The reason for using "api" in the route is to avoid collisions with ASP.NET MVC routing. That way, you can have "/contacts" go to an MVC controller, and "/api/contacts" go to a Web API controller. Of course, if you don‘t like this convention, you can change the default route table.

Once a matching route is found, Web API selects the controller and the action:

  • To find the controller, Web API adds "Controller" to the value of the {controller} variable.
  • To find the action, Web API looks at the HTTP verb, and then looks for an action whose name begins with that HTTP verb name.

           For example, with a GET request, Web API looks for an action prefixed with "Get", such as "GetContact" or "GetAllContacts".

           This convention applies only to GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH verbs.

           You can enable other HTTP verbs by using attributes on your controller. We‘ll see an example of that later.

  • Other placeholder variables in the route template, such as {id}, are mapped to action parameters.

Why is HttpGet required only for some actions?

原文地址:https://www.cnblogs.com/chucklu/p/10283664.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved