- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经阅读了大量描述相同错误消息的类似帖子,但它们似乎与我遇到的情况不符。
我最近开始使用 Web API 并将我所有的 MVC 方法从我返回 JSON 等的地方删除,所以 MVC 将只呈现 html,我将通过我的 webapi Controller 中的 ajax 调用模型。
奇怪的是,我可以从我的 Home apiController 进行 GET 和 POST(这样我就可以登录/注册等),但我只能从我创建的区域中的 API Controller 进行 GET。我得到一个 405(方法不允许),即使它的装饰和调用方式与其他 Controller 相同。我想路由没问题,否则它不会返回我的初始 get?
路由
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultAreaApi",
routeTemplate: "api/{area}/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Controller
// Returns Model
[HttpGet]
public HttpResponseMessage SelectAgent()
// The requested resource does not support http method 'POST'.
[HttpPost]
public HttpResponseMessage SelectAgent(Guid id)
JQuery
// Works fine
$.ajax({
type: 'POST',
url: '/api/Home/Login',
headers: options.headers,
contentType: "application/json; charset=utf-8",
dataType: 'JSON',
data: ko.toJSON(self.serverModel),
success: function (response) {
// Works fine
$.getJSON("/api/Account/Users/SelectAgent", function (model) { ....
// 405
$.ajax({
type: 'POST',
url: '/api/Account/Users/SelectAgent',
headers: options.headers,
contentType: "application/json; charset=utf-8",
dataType: 'JSON',
data: "{'id':'" + selectModel.agentId() + "'}",
success: function (response) {....
传递的数据看起来很好(或者至少它是针对它过去使用的 MVC Controller )。
我根本没有修改 Home API Controller ,我不明白我怎么能发布到它而不是我的其他 Controller 。啊。
任何指向正确方向的指示都会很棒。
最佳答案
Web API 仅查看基本数据类型的查询字符串参数。因此,当您发帖时,您的帖子只是查看 /api/Account/Users/SelectAgent
的 url。与函数匹配时不考虑提交的数据,因为您没有使用 [FromBody]
属性标记 Guid
。因此返回“不允许的方法”错误,因为它正在将您的 POST 请求发送到您的 GET 方法(无参数)。
您可以在 asp.net 上阅读更多相关信息:
If the parameter is a “simple” type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string. (More about type converters later.)
要解决此问题,请尝试执行以下操作之一:
更改您提交的 url 以在电子邮件的查询字符串中包含 id
url: '/api/Account/Users/SelectAgent?id=' + selectModel.agentId()
更改 Action 签名以读取 Id FromBody
:
public HttpResponseMessage SelectAgent([FromBody]Guid id)
关于c# - WebApi2 请求的资源不支持post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21905023/
我想为我的 ABP 项目中的特定应用程序服务关闭自动 WebAPI 生成服务。 最佳答案 RemoteService 属性可用于将类标记为远程服务或禁用固有地实现 IRemoteService 接口(
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我无法从 postman 访问 webapi 错误是下一个: 如您所见,没有授权。 valuescontroller.cs 是: namespace CVService.Controllers {
我有以下 Controller ,它应该接受用户名和密码作为 POST 中的有效负载。如果我将其更改为 HttpGet 就可以了。 [RoutePrefix("api")] public class
使用以下路线: routes.MapHttpRoute( name: "Set", routeTemplate: "api/set/{id}",
我正在使用 AngularJS,我正在尝试将 json 从我的服务发送到 webAPI Controller 。当我通过发送时,我在 webApi 函数的参数中收到 null。 我的功能服务是: an
据我了解,如果我有一个 ASP.NET WebApi 方法,其签名如下所示...... public HttpResponseMessage PostCustomer(Customer custome
我遇到了一个解决方案问题,我使用 Visual Studio SPA 模板中的部分在具有 Oauth 身份验证的 WebApi 中拥有帐户 Controller 。 app.UseOAuthBea
我按照此处的说明将 webApi.HelpPage 区域和 View 添加到使用 structureMap 的现有项目中 - 但是在访问/Help url 时: StructureMap Except
我有一个 WebAPI。如何返回并打开网页。例如,我想打开 CNN.com 页面。 [HttpGet] public HttpResponseMessage Get()
我想知道是否有人可以澄清这一点。我发现用法令人困惑。 链接和视频都没有回答我的问题 我知道像这样的链接 asp.net core middleware vs filters 甚至还有关于它的视频 但是
运行以下最新版本(在撰写本文时): Visual Studio 2019 16.4.5 .NET 核心 SDK 3.1.102 x64 测试的浏览器: 谷歌浏览器 80.0.3987.122 火狐 7
想法是,将有一个外部实体 (SharePoint) 调用我的 WebAPI 并传入 PDF 文件以及有关该 PDF 文件的一些额外元数据信息。我被困在如何构造 Web API 方法的签名上。这是我到目
我有一个 WebApi 服务处理来自简单表单的上传,如下所示: 但是,我不知道如何使用 HttpClient API 模拟同一
嗨,我是 Angular 的新手,现在我从一个示例登录页面开始,该页面传递包含用户名和密码的 userEntity。userEntity 是一个对象/实体是 webapi。 问题:当我为登录按钮单击
我有一个 AngularJS + MVC + WebAPI,我正在尝试:- 使用标准(个人账户)进行MVC认证;- 使用相同的用户和密码进行基于 WebAPI 的身份验证。 问题,AngularJS
Web API 的版本存在一些混淆。看看这个 Web API at NuGet , Microsoft ASP.NET Web API 2.2 5.2.3 什么?这里是没有提到 2.2 的描述 我的猜
我正在开发一个 Web 应用程序,该应用程序将 Owin 托管用于 MVC 和 WebApi 2。 我最近将 Microsoft Mvc/WebApi 包从 5.2.2 版升级到 5.2.3 版,将
随着Web技术的发展,现在各种框架,前端的,后端的,数不胜数。全栈工程师的压力越来越大。 现在的前端的框架,既可以做各种Web,又可以做各种APP,前端框架更新换代越来越快,越来越多。 传统的模
1. WebAPI 背景知识 1.1 什么是 WebAPI JS 分成三个大的部分: ECMAScript: 基础语法部分 DOM API: 操作页面结构 BOM API: 操作浏览器 WebAPI
我是一名优秀的程序员,十分优秀!