webapi路由问题
相关配置和代码如下:
1 WebApiConfig:使用默认路由,暂未增加{action}参数
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
2 StaffsController:
// GET: api/Staffs
public IEnumerable<Staff> GetAllStaffs()
{
return repository.GetAll();
}
// GET: api/Staffs/5
public Staff GetStaff(string staffCD)
{
Staff item = repository.Get(staffCD);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
3 前端调用处代码:
var sStaffCD = 'S1';
$.ajax({
url: "/api/Staffs/" + sStaffCD,
type: "GET",
contentType: "application/json; charset=urf-8",
success: function (data) {
...
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
...
}
});
问题: 每次都路由到GetAllStaffs而非GetStaff,为什么?
...全文
相关推荐
为ASP.NET MVC及WebApi添加路由优先级
一、为什么需要路由优先级 大家都知道我们在Asp.Net MVC项目或WebApi项目中注册路由是没有优先级的,当项目比较大、或有多个区域、或多个Web项目、或采用插件式框架开发时,我们的路由注册很可能 不是写在一个文件中的,而是分散在很多不同项目的文件中,这样一来,路由的优先级的问题就突显出来了。 比如: App_Start/RouteConfig.cs中 routes.MapRoute( name: Default, url: {controller}/{action}/{id}, defaults: new { controller = Home, action
C# WebApi 路由机制剖析
主要介绍了C# WebApi 路由机制剖析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
webApi 代码及测试工具
webApi 代码实例及测试工具,非常好用。可用于调试get,post,put,delete相关协议及接口。超好用。
webapi 项目代码及winform下调用接口
webapi 项目平台,包含接口生成,及接口调用方法,项目代码清晰明了,非常值得初学者参考借鉴
webapi.zip
webapi 从创建到数据获取、路由、跨域等示例及笔记资料


