我是新的使用web api,我试图调用我的控制器中的特定方法。web api路由与行动和编号
我
Global.asax的
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
的WebApiConfig这些路线
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new
{
id = RouteParameter.Optional
}
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new
{
action="DefaultAction",
id = RouteParameter.Optional
}
);
和我的控制器类
[HttpGet]
public HttpResponseMessage GetPatSummary(string PatId)
{
PatientSummary Pat = new PatientSummary();
HttpResponseMessage Response = new HttpResponseMessage();
string yourJson = Pat.GetPatient(PatId);
Response = this.Request.CreateResponse(HttpStatusCode.OK, yourJson);
return Response;
}
[ActionName("DefaultAction")] //Map Action and you can name your method with any text
public IHttpActionResult GetPatient(int id)
{
Object Obj = new object();
if (Obj!=null)
{
return NotFound();
}
return Ok(Obj);
}
我使用的URL是
http://localhost/mdmwapi/api/MdmwPatientController/GetPatSummary/sgdgdgddhdhd1334254
,但我得到这个错误 路径段不能包含连续的两个参数。它们必须用'/'或文字串隔开。
我越来越螺母:-)使用
http://localhost/api/MdmwPatientController/GetPatSummary/yourpatid
你也可以使用属性映射任何URL
什么是URL的'mdmwapi'部分? – Mostafiz
是项目名称 – brillox
你应该删除这部分,并再次尝试发生什么,因为它不是url的一部分 – Mostafiz