asp.net Web Api路由不工作
发布时间:2020-05-23 03:36:27 所属栏目:asp.Net 来源:互联网
导读:这是我的路由配置: config.Routes.MapHttpRoute( name: ActionApi, routeTemplate: api/{controller}/{action}/{id}, defaults: new { id = RouteParameter.Optional }); 而且,这是我的控制器: public class Product
|
这是我的路由配置: config.Routes.MapHttpRoute(
name: "ActionApi",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional }
);
而且,这是我的控制器: public class ProductsController : ApiController
{
[AcceptVerbs("Get")]
public object GetProducts()
{
// return all products...
}
[AcceptVerbs("Get")]
public object Product(string name)
{
// return the Product with the given name...
}
}
当我尝试api / Products / GetProducts /,它的作品. api /产品/产品?name = test也可以使用,但是api /产品/产品/测试不起作用.我究竟做错了什么? 更新: 以下是当我尝试api /产品/产品/测试时所得到的: {
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:42676/api/Products/Product/test'.","MessageDetail": "No action was found on the controller 'Products' that matches the request."
}
解决方法这是因为您的路由设置及其默认值.你有两个选择.1)通过更改路由设置以匹配Product()参数以匹配URI. config.Routes.MapHttpRoute(
name: "ActionApi",routeTemplate: "api/{controller}/{action}/{name}",// removed id and used name
defaults: new { name = RouteParameter.Optional }
);
2)其他推荐的方法是使用正确的方法签名属性. public object Product([FromUri(Name = "id")]string name){
// return the Product with the given name
}
这是因为该方法在请求时期望参数idapi /产品/产品/测试,而不是寻找名称参数. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – WCF中的405方法不允许错误
- asp.net-core – 如何在使用asp.net 5时更改登录URL
- 在asp.net mvc中绑定缺少元素的数组
- .net – Castle Windsor有没有什么缺点?
- iis – ASP / ASP.NET处理写权限的最佳方法是什么?
- asp.net-mvc-3 – 从单控制器操作返回多个部分视图?
- 如何删除IIS自定义标头像X-Powered-By:ASP.NET从响应?
- ASP.NET框架中的异步页面 – 其他线程在哪里,如何重新连接?
- asp.net-mvc – Donut hole缓存 – 排除MiniProfiler.Rende
- ASP.NET MVC3 AJAX.BeginForm AjaxOptions OnSuccess OnFai
推荐文章
站长推荐
热点阅读
