2
在我正在處理的asp.net mvc 5 + web api 2中,我定義的一些webapi路由不工作,完美地工作。我似乎無法確定問題所在。在你問及之前,我已經閱讀了所有的SO問題,並應用了我能找到的所有解決方案,但似乎沒有一種方法適用於我目前的情況。我也檢查,雙重檢查,但我不明白爲什麼。以下是我認爲會影響web api的一些配置和路由註冊。我的一些基於asp.net api屬性的路由返回404(未找到)
路由定義
[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
}
}
如果你能夠顯示路由器工作的控制器的代碼,然後在沒有控制器的地方編寫代碼,那就太好了。 另外,你所有的路由屬性爲基礎,還是你有一些配置? – Martin
好的,讓我編輯這個問題。 – Cizaphil
將參數定義爲最後一個標記是沒有意義的:'[Route(「FollowApi/FollowClient/{profileId:int}」)]'? – Alexei