asp.net-web-api – Webapi方法获取字符串参数未被调用
发布时间:2020-12-16 09:17:30 所属栏目:asp.Net 来源:网络整理
导读:我用两个get方法创建asp.net webapi.一个返回所有记录,而另一个应根据名为countrycode的字符串参数进行过滤.我不确定为什么使用字符串参数的get方法不会被调用. 我试过以下的uri http://localhost:64389/api/team/'GB'http://localhost:64389/api/team/GB 以
我用两个get方法创建asp.net webapi.一个返回所有记录,而另一个应根据名为countrycode的字符串参数进行过滤.我不确定为什么使用字符串参数的get方法不会被调用.
我试过以下的uri http://localhost:64389/api/team/'GB' http://localhost:64389/api/team/GB 以下是我的代码 Web API public HttpResponseMessage Get() { var teams = _teamServices.GetTeam(); if (teams != null) { var teamEntities = teams as List<TeamDto> ?? teams.ToList(); if (teamEntities.Any()) return Request.CreateResponse(HttpStatusCode.OK,teamEntities); } return Request.CreateErrorResponse(HttpStatusCode.NotFound,"Team not found"); } public HttpResponseMessage Get(string countryCode) { if (countryCode != null) { var team = _teamServices.GetTeamById(countryCode); if (team != null) return Request.CreateResponse(HttpStatusCode.OK,team); } throw new Exception(); } WebAPIConfig public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional } ); config.Formatters.JsonFormatter.SupportedMediaTypes .Add(new MediaTypeHeaderValue("text/html")); } } 解决方法
我想你可能会从默认的API路由中使用默认的’Get()’方法.
我希望如果您在方法上将参数名称更改为“id”,那么它也会起作用: public HttpResponseMessage Get(string id) 这是因为默认路由中的可选参数名称是“id”. 要使属性路由起作用,您需要使用先前由路由配置推断的值来装饰控制器和方法. [RoutePrefix("api/team")] public class TeamController : ApiController 然后在你的第二个get方法之上: [Route("{countryCode}")] public HttpResponseMessage Get(string countryCode) 由于属性路由,我没有使用“旧式”路由. 编辑评论: [HttpGet()] [Route("byTeamName/{teamName}")] public HttpResponseMessage GetByTeamName(string teamName) Your url would then be /api/team/byTeamName/... 您的其他方法名称为“Get”,默认HTTP属性路由查找与HTTP谓词相同的方法名称.但是,您可以根据自己喜欢的方式命名方法,并使用动词来装饰它们. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 什么是asp.net会话超时的单位?
- asp.net – 这是一个RESTFUL MVC Web服务吗?
- asp.net-mvc – 如何使用wmd-editor控件检索markdown和html
- asp-classic – 使用SMTP身份验证的经典ASP发送电子邮件
- asp.net-mvc – asp.net mvc – 缓存
- asp.net-core-mvc – 使用MVC Core下载文件
- asp.net – 从承载令牌(Web API)获取自定义声明值
- asp.net-mvc – 使用Fluent验证进行不显眼的自定义/条件验证
- asp.net-mvc – NHibernate和上下文实体
- asp.net-mvc – 在ASP.NET MVC 3中使用Ajax响应发送的自定义
推荐文章
站长推荐
- asp.net-mvc – 什么是ASP.Net MVC?
- ASP.NET MVC twitter / myspace样式路由
- asp.net – 为什么我不能从Visual Studio 2005安
- ASP.Net – 在没有Windows用户的情况下使用基本身
- 找出ASP.NET WebForms中控件的生命周期的哪个阶段
- 如何打开一个页面在新的选项卡按钮点击asp.net?
- asp.net – 母版页和嵌套表单问题
- 从ASP.NET中的web.sitemap文件构建菜单
- asp.net-core – 带有ASP.NET身份3的JWT承载令牌
- asp.net-mvc – ASP.NET MVC 4移动显示模式停止工
热点阅读