c# - 我的一些基于 asp.net api 属性的路由返回 404(未找到)

标签 c# asp.net asp.net-mvc-5 asp.net-web-api2

在我正在处理的 asp.net mvc 5 + web api 2 中,我定义的一些 webapi 路由无法正常工作,而有些则可以正常工作。我似乎无法确定问题出在哪里。在你问之前,我已经阅读了整个 SO 问题并应用了我能找到的所有解决方案,但在我目前的情况下似乎都不起作用。我也检查过,仔细检查过,但我不明白为什么。下面是一些我认为影响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>

如果我在时间有限的情况下,请提供有关此问题发生原因的任何帮助或指示,我们将不胜感激。

编辑

工作接口(interface)

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

最佳答案

看起来 Controller 上的 RoutePrefix 没有包含在客户端调用中。我从中得出的结论是,有一种特定的模式来解决这类问题是有意义的。 像这样的东西:

  • 网址是否正确?
  • httpMethod 是否正确?
  • 参数命名是否正确?

关于c# - 我的一些基于 asp.net api 属性的路由返回 404(未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111652/

相关文章:

c# - 为什么我应该使用 SecureString 而不是字符串?

没有 dll 的桌面 C# 数据库

asp.net-mvc - 如何从 ID MVC 5 的另一个表中获取名称?

iframe - Azure AD Owin 与 Iframe 解决方案

c# - MSMQ:事务上下文中的远程访问

c# - 何时触发自定义控件事件?

asp.net - Web服务中的Response.Write()

c# - 两个命名空间之间的不明确引用错误

c# - 业务应用程序 : Will F# make my life easy?

razor - VS2013 Razor 及格式化文档