[英]Why Is My Request Going to the Wrong Route?
My request is working, but it is not going to the right route. 我的请求正在工作,但是没有正确的路线。 I don't understand why.
我不明白为什么。
I am receiving my '200 ok' response when I start the project but it is at the wrong route. 启动项目时,我收到“ 200 ok”响应,但路线错误。
I want the route http://localhost:4047/api/[controller]
but instead http://localhost:4047/
is working! 我想要路由
http://localhost:4047/api/[controller]
但是http://localhost:4047/
正常工作! No where am I specifying this route. 不,我在哪里指定这条路线。
Here is the controller.
这是控制器。
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Route("api/[controller]")]
public class MenuController : ApiController
{
private IMenuRepo xMenuRepo;
public MenuController(IMenuRepo iopt)
{
xMenuRepo = iopt;
}
[HttpGet]
[Route("")]
public HttpResponseMessage GetOk()
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
Here is the WebApiConfig
这是WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}"
);
}
Here is the Route Config
这是路由配置
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "api/{controller}",
defaults: new { controller = "Home" }
);
}
As far as I can tell, api/Menu
should be the correct route. 据我所知,
api/Menu
应该是正确的路由。
Yes you are specifying the route 是的,您正在指定路线
//GET /
[HttpGet]
[Route("")]
public HttpResponseMessage GetOk()
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
The controller needs a route prefix, while example is only specifying a controller route. 控制器需要路由前缀,而示例仅指定了控制器路由。
Change to: 改成:
[EnableCors(origins: "*", headers: "*", methods: "*")]
[RoutePrefix("api/Menu")]
public class MenuController : ApiController {
private IMenuRepo xMenuRepo;
public MenuController(IMenuRepo iopt) {
xMenuRepo = iopt;
}
//GET api/Menu
[HttpGet]
[Route("")]
public HttpResponseMessage GetOk() {
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
The syntax in the OP [Route("api/[controller]")]
is for asp.net-core OP
[Route("api/[controller]")]
的语法适用于asp.net-core
Source: Attribute Routing in ASP.NET Web API 2 来源: ASP.NET Web API 2中的属性路由
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.