gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:11 25 4
gpt4 key购买 nike

这是我第一次尝试在项目中使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com