我的某些基于 asp.net api 属性的路由返回 404(未找到)
Some of my asp.net api attribute based routing are returning 404 (not found)
在我正在处理的 asp.net mvc 5 + web api 2 中,我定义的某些 webapi 路由无法正常工作,而有些则可以正常工作。我似乎无法确定问题出在哪里。在您提问之前,我已经阅读了整个 SO 问题并应用了我能找到的所有解决方案,但其中 none 似乎适用于我目前的情况。我也检查过,仔细检查过,但我不明白为什么。下面是一些我认为影响web的配置和路由注册api.
路由定义
[HttpPost]
[Route("FollowApi/{profileId:int}/FollowClient" Name = "FollowClient")]
Application_start配置
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Webconfig 配置
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
拜托,任何关于为什么会出现此问题的帮助或指示将不胜感激,因为我处于时间有限的情况下。
编辑
工作api
public class CommonApiController : BaseApiController
{
[Authorize]
[HttpGet]
[Route("Client/GetInfoCounts")]
public IHttpActionResult GetInfoCounts()
{
//Method body
}
}
不工作api
public class FollowApiController : BaseApiController
{
[HttpPost]
[Route("FollowApi/{profileId:int}/FollowClient")]
public IHttpActionResult Follow(int profileId)
{
//Method body
}
}
控制器上的 RoutePrefix 似乎未包含在客户端调用中。我从中得出的结论是,有一种特定的模式来解决这类问题是有意义的。
类似于:
- url 正确吗?
- httpMethod 是否正确?
- 参数命名是否正确?
在我正在处理的 asp.net mvc 5 + web api 2 中,我定义的某些 webapi 路由无法正常工作,而有些则可以正常工作。我似乎无法确定问题出在哪里。在您提问之前,我已经阅读了整个 SO 问题并应用了我能找到的所有解决方案,但其中 none 似乎适用于我目前的情况。我也检查过,仔细检查过,但我不明白为什么。下面是一些我认为影响web的配置和路由注册api.
路由定义
[HttpPost]
[Route("FollowApi/{profileId:int}/FollowClient" Name = "FollowClient")]
Application_start配置
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Webconfig 配置
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
拜托,任何关于为什么会出现此问题的帮助或指示将不胜感激,因为我处于时间有限的情况下。
编辑
工作api
public class CommonApiController : BaseApiController
{
[Authorize]
[HttpGet]
[Route("Client/GetInfoCounts")]
public IHttpActionResult GetInfoCounts()
{
//Method body
}
}
不工作api
public class FollowApiController : BaseApiController
{
[HttpPost]
[Route("FollowApi/{profileId:int}/FollowClient")]
public IHttpActionResult Follow(int profileId)
{
//Method body
}
}
控制器上的 RoutePrefix 似乎未包含在客户端调用中。我从中得出的结论是,有一种特定的模式来解决这类问题是有意义的。 类似于:
- url 正确吗?
- httpMethod 是否正确?
- 参数命名是否正确?