当我请求没有参数的路由时,我的web api工作正常,但是当我添加参数时它找不到控制器:
这是我的WebApiConfig.cs:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "v1/{controller}/{id}",
defaults: new { area = "v1", id = RouteParameter.Optional }
);
这是我在区域“v1”中的控制器:
[RoutePrefix("v1/Dummy")]
public class DummyController : ApiController
{
[HttpGet]
//[Route("")]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
[HttpGet]
[Route("{id:int}")]
public string Get(int id)
{
return "value";
}
[HttpPost]
public void Post([FromBody]string value)
{
}
[HttpPut]
public void Put(int id, [FromBody]string value)
{
}
[HttpDelete]
public void Delete(int id)
{
}
}
有人可以帮忙吗?
编辑:
我在Application_Start()中有以下代码:
AreaRegistration.RegisterAllAreas();
System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);
编辑2:
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("SumiService/Customer", new WebServiceHostFactory(), typeof(SumiyCustomerService)));
RouteTable.Routes.Add(new ServiceRoute("SumiService", new WebServiceHostFactory(), typeof(SumiService)));
RouteTable.Routes.Add(new ServiceRoute("goops", new WindsorServiceHostFactory<RestServiceModel>(), typeof(IGoOpsService)));
RouteTable.Routes.Add(new ServiceRoute("TempService", new WebServiceHostFactory(), typeof(TempService)));
RouteTable.Routes.Add(new ServiceRoute("Go", new WindsorServiceHostFactory<RestServiceModel>(), typeof(GoBusinessContracts.IGoService)));
}
答案 0 :(得分:0)
尝试将 [FromRoute] 或 [FromUri] 属性添加到您的ID参数中:
[HttpGet]
[Route("{id:int}")]
public string Get([FromRoute] int id)
{
return "value";
}