问题标签 [asp.net-web-api-routing]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
259 浏览

asp.net-web-api - Web.API 在某些部署中有效,但在其他部署中返回 404

我有一个广泛分布的 ASP.NET WebForms 应用程序。我的新版本添加了 Web.API,用户一直在玩 beta。在大多数 beta 安装中,一切正常,但对于某些安装,所有 Web.API 调用都返回 HTTP 404 Not Found 错误。我无法弄清楚为什么 Web.API 调用在某些服务器上失败但在其他服务器上运行良好。

我的猜测是某种服务器配置破坏了路由,但我不知道它是什么。我什至已经 RDP'd 进入这些站点之一,但找不到任何明显的东西。

这是一个示例 API 调用:

获取http://site.com/api/events/27

编码:

路由定义,从名为 GspHttpApplication 的自定义 HTTP 模块的 Init 事件调用:

上面的示例 GET 应该匹配名为 GalleryApi2 的路由,就像我在大多数安装中所说的那样。

我知道 WebDAV 会引起麻烦(405 错误),所以我已经在 web.config 中删除了它:

因此,这一切都归结为弄清楚为什么此配置适用于某些 Web 安装而不适用于其他安装。

罗杰

0 投票
1 回答
1620 浏览

.net - 如何配置 Web Api 以从 url 参数中选择响应格式?

我正在尝试配置 web api 区域以允许我传递可选的格式扩展名。这是我RegisterArea的方法的代码:

现在,如果我去http://www.example.com/api/countries它工作正常,但是当我尝试http://www.example.com/api/countries.json给我

我究竟做错了什么?

0 投票
2 回答
967 浏览

asp.net-mvc - ASP .Net MVC 中多个设备的多视图

什么是构建 ASP .Net MVC 项目的最佳方式,该项目具有通用的 Web 服务部分和多个设备的多个视图。我从一个 JQuery 移动项目开始,并且有移动设备的视图。现在我需要添加应该作为 Web 服务工作的 Web api 控制器。桌面浏览器还需要另一个 View 部件。

分配不同的区域来分组不同的视图是否很好?

网络请求流程:

  1. 将移动客户端请求路由到Mobile Area中的MVCController,桌面客户端请求从RouteConfig路由到Desktop Area中的MVCController
  2. 从 MVC 控制器调用 web api 控制器(通用)
  3. 从 MVC 控制器返回对应的 View

有没有更好的方法可以做同样的事情?如果同一项目中的 Web 服务和多个视图会增加复杂性,我会感到困惑。

0 投票
2 回答
7876 浏览

json - 使用 web api 控制器 MVC 返回 JSON

我正在尝试将我正在使用的常规旧控制器转换为 API 控制器并且遇到了一些困难。这一系列函数的作用是,在 jQuery 中,它遍历包含员工所有用户名的文件,并且对于每个用户名,它调用我的 webapi 控制器中的 PopulateEmployee 方法,该方法应该返回 JSON,然后填充结果 div。

当手动导航到 ..domain../staffinformation/populateemployee/employeeusername

我得到错误

请注意,它将填充的 div 是 Umbraco CMS 页面中的部分视图,我认为这不是问题,但如果你们有不同的想法,请告诉我。

webAPI路由或其他东西我必须缺少一些东西。

谢谢你的帮助。

这是代码。

请注意这个方法有 HttpPost 标签

为 api 控制器设置的路由

指定使用 'POST' 的 jQuery 调用,请原谅此函数中递归调用的棘手之处。

0 投票
2 回答
7948 浏览

asp.net-web-api - Asp.Net WebAPI 和 AttributeRouting - 没有重载的属性构造函数?

我刚刚下载了用于 WebAPI 的 AttributeRouting NuGet 包,并且在我的控制器中出现了问题。我认为使用它的方法是有类似的东西:

但是我收到以下错误:错误 2 'System.Web.Http.HttpGetAttribute' 不包含采用 1 个参数的构造函数

我需要尽快启动并运行它。HttpGet 属性没有重载构造函数有什么原因吗?

更新

在路线:

当我点击 'api/inbox/EnquiryCount' 的 URL 时,我收到此错误:

0 投票
3 回答
2948 浏览

c# - ASP.NET MVC4 Web API 控制器卡在一条路线上

我创建了一个新的 ASP.NET MVC4 Web Api 项目。除了默认的 . 之外ValuesController,我还添加了另一个控制器ScenarioController. 它具有与 完全相同的方法ValuesController。但由于某种原因,它的行为有所不同。

使用断点,我知道/api/scenario/1实际上被发送到public IEnumerable<string> Get(),而不是预期的public string Get(int id). 为什么?

作为参考,这里是相关文件(这些是原始的默认 mvc4-webapi 类,没有修改任何东西):

全球.asax.cs

WebApiConfig.cs

值控制器.cs

ScenarioController.cs(是的,它在 Controllers 文件夹中)

0 投票
1 回答
51 浏览

asp.net-web-api - aspnet webapi method that takes Project/42/Children - how?

I can't figure out how to write the method signature for a REST(ish) AJAX call in Aspnet WebAPI.
My route is recognised but I get a "No HTTP resource was found that matches the request URI..."

I try to do a REST(ish) call like

and my idea is to have the server return all children of project 42.

My route is:

and my method signature is:

Why isn't my method recognised?
I am also not sure I am doing the right "restish" thing here.

0 投票
3 回答
2321 浏览

asp.net-web-api - 一个带有两个 get 方法的 aspnet webapi 控制器并获取多个与请求匹配的操作

打电话时

对于下面的路由和方法,我得到“找到与请求匹配的多个操作...Get(Int32)...Children(Int32)”

打电话时

正确返回。

我的路由是:

还有我的方法:

我认为不指定操作会导致 Webapi 默认使用方法 (GET)。现在看起来 Webapi 正在运行不考虑名称的方法,只寻找参数匹配。我真的不相信这一点,并认为我在其他地方有过错。

0 投票
2 回答
2221 浏览

c# - 根据 json 值路由到不同的操作

我想根据特定 json 参数的值将请求路由到不同的操作。

例如,给定以下 json 数据:

我希望能够对我的 ApiController 进行 2 种不同的操作:

这样的事情怎么能做到?

更新:

如果可能的话,我想要相同的 URL。类似的东西/objects/create

0 投票
2 回答
21083 浏览

asp.net-web-api - Web API get route values

Is there any way to statically get route values from a service method (outside of a controller) that is running in a Web API context? For example, I can do the following in ASP.NET MVC:

I'd like to find the equivalent version of this code for Web API.

When I try to debug a sample Web API request and look at HttpContext.Current.Handler it is of type HttpControllerHandler, but this type doesn't have any properties to access route data.

EDIT

To try to help provide some more information. The code I am trying to read the value from is inside of a factory class I have that builds a custom object for my application.