bri*_*lox 2 c# asp.net-web-api
我是使用 Web api 的新手,我正在尝试调用控制器中的特定方法。
我有
全局.asax
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
Run Code Online (Sandbox Code Playgroud)
具有这些路由的 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
}
);
Run Code Online (Sandbox Code Playgroud)
和我的控制器
[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);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用的网址是
http://localhost/mdmwapi/api/MdmwPatientController/GetPatSummary/sgdgdgddhdhd1334254
但我收到此错误路径段不能包含两个连续的参数。它们必须用“/”或文字字符串分隔。
我快疯了:-)
使用属性路由
[HttpGet]
[Route("api/MdmwPatientController/GetPatSummary/{PatId}")]
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;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用请求它
http://localhost/api/MdmwPatientController/GetPatSummary/yourpatid
Run Code Online (Sandbox Code Playgroud)
您也可以通过这种方式使用属性路由映射任何 url
| 归档时间: |
|
| 查看次数: |
2790 次 |
| 最近记录: |