c# - 本地测试 web api 时出现 404 not found 错误

标签 c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing

这是我第一次尝试在项目中使用 WEB API,但没有成功......

当我尝试在 Fiddler 中访问我的 api 路由时,我不断收到 404 错误。

我尝试在网络上甚至这里的以下链接中查找很多内容,但组合太多,我不确定哪种方法可行。

HTTP 404 Page Not Found in Web Api hosted in IIS 7.5

如果有人可以帮助我获得正确的设置,我将真的感激不尽。

这是我的代码: Web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="FuelTicketImageRetrievalSvc.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="AssignedGroup" value="FMS Maintenance Level 3" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <system.webServer>
    <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>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.serviceModel>
    <bindings />
    <client />
  </system.serviceModel>
  <applicationSettings>
    <FuelTicketImageRetrievalSvc.Properties.Settings>
      <setting name="FuelTicketImageRetrievalSvc_IncidentService_HPD_IncidentInterface_Create_WSService" serializeAs="String">
        <value>http://miavsbremweb/arsys/services/ARService?server=miavsbremapp.ryder.com&amp;webService=HPD_IncidentInterface_Create_WS</value>
      </setting>
    </FuelTicketImageRetrievalSvc.Properties.Settings>
  </applicationSettings>
</configuration>

WebApiConfig.cs 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace FuelTicketImageRetrievalSvc
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

Global.asax.cs 文件:

using FuelTicketImageRetrieval;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace FuelTicketImageRetrievalSvc
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

我尝试调用的 Controller 方法。它只是一个虚拟方法,它只返回没有参数的 null。它的名称是 FuelTicketImageRetrievalController:

public string GetSpecificFuelTicket()
        {
            try
            {
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

我的项目名为 FuelTicketImageRetrievalSvc。我已经通过项目中的 Web 设置验证正在使用 IIS Express 并设置为

http://localhost:11581/

url路径调用。

http://localhost:11581/FuelTicketImageRetrievalSvc/api/GetSpecificFuelTicket

最佳答案

你不需要 FuelTicketImageRetrievalSvc 在 uri 中,它应该可以简单地与 /api/... 一起工作 这就是您的路线匹配的内容,那里有 svc 名称会导致它不匹配。

关于c# - 本地测试 web api 时出现 404 not found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23416409/

相关文章:

asp.net-mvc - 多选列表未使用 linq2sql 在 c# mvc 中显示所选项目

javascript - 我需要禁用提交按钮,直到所有字段都有值

c# - 在网页中显示来自数据库的图像

c# - 无法从 ASP.NET 连接到 MySQL 数据库

c# - SQL 插入不工作

asp.net - 使用自定义路径时忽略 ServiceStack web.config 设置

asp.net-mvc - .NET MVC 防止伪造 POST

c# - 如何在 Simple Injector 3.0.0 版中定义 DefaultScopedLifestyle?

c# - Linq扩展方法,如何在集合递归中查找子项

c# - 当 source 上的属性为 false 时是否可以映射到 null?