【问题标题】:Asp.Net Web Api is not working in Godaddy server but its working fine in localAsp.Net Web Api 在 Godaddy 服务器上不工作,但在本地工作正常
【发布时间】:2016-09-13 11:27:21
【问题描述】:

当我在 godaddy 服务器上运行 Web Api (www.example.com/api/values) 时出现错误,它显示 (404 Not Found The requested document was not found on this server.) .但它在本地运行良好。

我已经在 Godaddy 中使用 FTP(FileZilla) 手动上传文件,Godaddy 服务器也支持 ASP.NET 托管。

以下是Web Api中使用的代码:

ValuesController.cs

namespace Webapi {
 public class ValuesController : ApiController {
    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<controller>
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    public void Delete(int id)
    {
    }
  }
}

Valueswebconfig.cs

namespace Webapi {
  public static class WebApiConfig {
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
        );

       // config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));


        //// Custom Formatters:
        //config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(
        //    config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"));

        //var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        //jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    }

  }
}

Web.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>

<system.web>
  <compilation debug="true" targetFramework="4.5.2" />
  <httpRuntime targetFramework="4.5.2" />
</system.web>

<system.webServer>
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer></configuration>

【问题讨论】:

  • 试试这样,www.example.com/api/api/values
  • 谢谢你,Pranav,我试过像上面一样,但它不起作用......请指教
  • 试试这个 www.example.com/api/Values/Get
  • 您很可能需要在服务器上的 IIS 上设置应用程序。
  • 以上链接也无法正常工作。嗨,谢谢,但是如何在 Godaddy 服务器中配置 IIS。我在 Godaddy 服务器上运行 web api...

标签: c# asp.net web-services rest asp.net-web-api


【解决方案1】:

GoDaddy Shared Hosting 不支持 Web API,因为它无法提供请求的 URL(无扩展名 URL)。 GoDaddy 上的 Windows 共享主机 (Plesk) 无法正常运行 API(即使它支持 .NET 和 IIS 等)。检查您是否可以访问主机公共目录中的 index.html 文件。如果您没有 index.html,您可以创建一个 index.html,然后将其部署到您在 GoDaddy 主机上的公共主管。

您可以在 GoDaddy 或 AZURE 上购买虚拟机。

【讨论】:

    猜你喜欢
    • 2014-06-19
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多