当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Some of my asp.net api attribute based routing are returning 404 (not found)

问题描述:

In the asp.net mvc 5 + web api 2 am working on, some of the webapi routes that I defined are not working, while some are working perfectly. I can't seem to identify what the problem is. And before you ask I have read the whole SO questions and applied all the solutions I can find, but none of it seems to work in my current situation. I also checked, double checked and yet I can't figure out why. Here are some of the configurations and route registrations that I think affects web api.

Route definition

[HttpPost]

[Route("FollowApi/{profileId:int}/FollowClient" Name = "FollowClient")]

Application_start configuration

AreaRegistration.RegisterAllAreas();

GlobalConfiguration.Configure(WebApiConfig.Register);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

RegisterRoutes(RouteTable.Routes);

Webconfig configuration

<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>

Please, any help or pointers as to why this problem is occurring will be seriously appreciated as I am in a time constrained situation.

Edit

Working api

public class CommonApiController : BaseApiController

{

[Authorize]

[HttpGet]

[Route("Client/GetInfoCounts")]

public IHttpActionResult GetInfoCounts()

{

//Method body

}

}

Not working api

public class FollowApiController : BaseApiController

{

[HttpPost]

[Route("FollowApi/{profileId:int}/FollowClient")]

public IHttpActionResult Follow(int profileId)

{

//Method body

}

}

网友答案:

It looks like the RoutePrefix on the controller was not included in the client call. My take away from this is that it makes sense to have a certain pattern for troubleshooting these kind of problems. Something like:

  • Is the url correct?
  • Is the httpMethod correct?
  • Are the parameters named correctly?
分享给朋友:
您可能感兴趣的文章:
随机阅读: