我正在使用WebApi 2 .Net Framework 4.5构建API和IIS(7.5集成)和IIS(8集成),以便部署到本地和实时环境。
我正在使用System.Web.Http.Cors进行跨域请求。
WebApi.Config
public static void Register(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.MapHttpAttributeRoutes();
config.Filters.Add(new UnhandledExceptionFilterAttribute());
config.BindParameter(typeof(DateTime), new DateTimeModelBinder());
}
身份验证资源
[DataContract]
public class Authentication : BaseResource
{
public Authentication()
{
}
[DataMember(EmitDefaultValue=false)]
public string UserName { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Password { get; set; }
}
身份验证控制器
[RoutePrefix("sessiontoken")]
public class AuthenticationController : ApiControllerBase
{
private readonly IAuthenticationManager _authenticationManager;
AuthenticationController()
{
}
public AuthenticationController(IAuthenticationManager authenticationManager)
: base(authenticationManager)
{
_authenticationManager = authenticationManager;
}
[HttpPost]
[Route("")]
public HttpResponseMessage Post(Authen.Authentication auth)
{
auth = _authenticationManager.CreatePermanentToken(auth);
return Request.CreateResponse<Authen.Authentication>(HttpStatusCode.Created, auth);
}
}
Web.Config(IIS 8集成模式)
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="false">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<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>
RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
我正在尝试执行发布请求,但所有请求都返回404。 我已经尝试了一些帖子中提到的所有配置更改。
Http请求
URL: http://api.x.com/sessiontoken
请求标题
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Connection: keep-alive
Content-Length: 0
Content-Type: application/json
Host: api.x.com
Origin: http://x.com
Referer: http://x.com/
本地IIS(7.5集成)的类似更改工作没有麻烦。 但是同样的更改不适用于IIS(8)集成模式的共享托管环境。
观察到,如果我执行发布并将文件夹映射到IIS并请求URI。
虚拟文件夹映射到已发布内容将返回404(本地IIS7.5)