我有以下网址来传递不同的API。
/shared/rendererjob -- I done
/shared/rendererjob/{jobId} -- I done
/shared/rendererjob/{jobId} -- done
/shared/rendererjob/{jobId}/status -- done
/shared/renderer/documentconverter/document -- I done
/shared/renderer/documentconverter/storage -- I done
/shared/renderer/documentconverter/callback -- I done
/shared/rendererhealth?q={level} -- **I dont know how to do this one**
如何为此编写webconfig.cs - / shared / rendererhealth?q = {level}
我的配置代码如下。
config.Routes.MapHttpRoute(
name: "RendererApi",
routeTemplate: "shared/{controller}/{renderGUID}",
defaults: new { action = "rendererJob", renderGUID =
RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "RendererAPiStatus",
routeTemplate: "shared/{controller}/{jobid}/status",
defaults: new { action = "getJobStatus", jobid = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DocumentConverterApi",
routeTemplate: "shared/renderer/{controller}/{action}"
);
以上代码适用于我所做的事情。 请告诉我如何使用参数配置控制器。
**How to achieve this path --
http://localhost:12345/shared/rendererhealth?q={level}**
答案 0 :(得分:0)
由于您的路线似乎非常依赖于控制器,我要做的第一件事就是更改您现有的路线,如下所示:
config.Routes.MapHttpRoute(
name: "RendererApi",
routeTemplate: "shared/rendererjob/{renderGUID}",
defaults: new { action = "rendererJob",
renderGUID = RouteParameter.Optional,
controller="rendererJob" }
);
config.Routes.MapHttpRoute(
name: "RendererAPiStatus",
routeTemplate: "shared/rendererjob/{jobid}/status",
defaults: new { action = "getJobStatus",
controller="rendererjob" }
);
config.Routes.MapHttpRoute(
name: "DocumentConverterApi",
routeTemplate: "shared/renderer/documentconverter/{action}",
defaults: new { controller="documentconverter" }
);
请注意,您不能在路径中间使用可选参数,因此我更改了RendererAPiStatus路线,以便jobid是必需的。
接下来,在新资源的末尾添加一条新路线:
config.Routes.MapHttpRoute(
name: "RendererHealthApi",
routeTemplate: "shared/rendererhealth",
defaults: new { controller="rendererhealth" }
);
然后,您可以添加新的控制器方法:
public class RendererHealthController : ApiController
{
public string Get(int q)
{
return "hello";
}
}
请注意,上面假设您使用的是GET请求,{level}是一个整数。