【发布时间】:2016-10-06 00:58:19
【问题描述】:
我是使用 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);
}
我使用的网址是
http://localhost/mdmwapi/api/MdmwPatientController/GetPatSummary/sgdgdgddhdhd1334254
但我收到此错误 一个路径段不能包含两个连续的参数。它们必须用“/”或文字字符串分隔。
我快疯了:-)
【问题讨论】:
-
网址的
mdmwapi部分是什么? -
是项目名称
-
您应该删除这部分并重试发生的情况,因为它不是 url 的部分
-
我做了,但由于 IIS 中的 url 注册为 localhost/mdmwapi,我得到了 404 错误我可以尝试重新创建虚拟目录为 localhost/api
-
所以我将虚拟目录重新创建到 localhost/api 以便完整的 URL 现在是 localhost/api/MdmwPatientController/GetPatSummary/… 但我得到相同的错误附加信息:路径段不能包含两个连续的参数。它们必须用“/”或文字字符串分隔。
标签: c# asp.net-web-api