【发布时间】:2015-01-05 06:09:20
【问题描述】:
我正在使用 Web-API 2 创建网络服务。我已经使用以下代码启用了跨域资源共享。
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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</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" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<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.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<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="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.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>
</assemblyBinding>
</runtime>
<connectionStrings>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
<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" />
<customErrors mode="Off"/>
</handlers>
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="*" /> </customHeaders> </httpProtocol>
</system.webServer></configuration>
API控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProActiveWebServices.Models;
using System.Web.Hosting;
using System.IO;
using System.Drawing.Imaging;
using System.Net.Http.Headers;
using System.Drawing;
using System.Net.Mail;
using System.Text;
namespace ProActiveWebServices.Controllers
{
public class EnquiryController : ApiController
{
ProActiveEntities db = new ProActiveEntities();
List<Enquiry> en = new List<Enquiry> { };
List<City> ct = new List<City> { };
EnquiryList enquiryList = new EnquiryList();
CityList cityList = new CityList();
// GET api/<controller>
[Route("api/GetAllEnquiry")]
public HttpResponseMessage Get()
{
try
{
if (CheckAuthentication(Request) || 1==1)
{
var enquiry = db.tbl_Enquiry.ToList();
foreach (var e in enquiry)
{
Enquiry _enquiry = new Enquiry();
_enquiry.FullName = e.FullName;
_enquiry.Id = e.EnquiryID;
_enquiry.CompanyName = e.CompanyName;
_enquiry.ContactAddress = e.ContactAddress;
_enquiry.Email = e.Email;
_enquiry.fk_CityID = e.fk_CityID;
_enquiry.Comment = e.Comment;
en.Add(_enquiry);
}
enquiryList.EnquiryLists = en;
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new { Enquiry = enquiryList });
return response;
}
else
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, new { Error = "Invalid Header and Authorization" });
return response;
}
}
catch (Exception ex)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, "Error:" + ex.ToString());
return response;
}
}
}
}
这是我的 WebAPIConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.Cors;
namespace ProActiveWebServices
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var cors = new EnableCorsAttribute("*", "*", "*");
// Web API routes
config.MapHttpAttributeRoutes();
config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
}
}
现在,当我在 localhost 上运行它时,一切正常。但是当在 godaddy 上托管 Web 服务时,我收到 500-Internal server 错误。 我也在 web.config 中启用了 Access-Control-Allow-Origin。但还是没有成功
【问题讨论】:
-
您是针对 OPTIONS 请求获得 500(这意味着与 Web.Config 中的动词相关的内容)还是针对实际的 GET/POST 请求?禁用自定义错误应该让您看到完整的调用堆栈 - 使用 Fiddler 拦截和检查请求并发布调用堆栈。
标签: c# asp.net asp.net-web-api