405:方法不允许。

6

编辑:总结一下,我的网络应用程序似乎可以被任何人(仅限火狐浏览器或谷歌浏览器)访问,我也可以使用我的主要计算机进行访问。如果我尝试从我的局域网中的任何其他计算机访问http://luiscarlosch.com/WebFormClean.aspx,它会出现405错误。

我可以从本地主机完美地调用WCF Web方法。我使用Visual Studio发布工具将其发布到此服务器:http://luiscarlosch.com/WebFormClean.aspx(仅限火狐浏览器或谷歌浏览器),并且它可以正常工作。问题是当我尝试从另一台计算机访问时。我得到了405:方法不允许的错误。 但这毫无意义,因为正如我所说,当我远程访问发布者计算机时,它可以正常工作。 有任何想法吗?

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ContactProxy
{
    [WebGet()]
    [OperationContract]
    public Contact getByID(int IDContact)
    {
        Contact contact = new Contact(IDContact);
        return contact;
    }
    [OperationContract]
    public EntityData insertEntityData(int IDEntityDataFieldType, int IDContact, String value)
    {
        //Contact contact = new Contact();
       // contact.insertEntityData(IDEntityDataFieldType, IDContact, value);
        EntityData entityData = new EntityData();
        entityData.save(IDEntityDataFieldType, IDContact, value);

        return entityData;
    }
}

这两种方法似乎都不起作用。

我刚刚发现一些用户可以通过更改值来访问http://luiscarlosch.com/WebFormClean.aspx。所以,有些客户端可以读取这些方法,而有些则不能。这不应该发生。

Web配置文件

<?xml version="1.0"?>

<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"/>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
  </system.web>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors" >
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="WebApplicationTest.WCFProxy.EmployeeProxyAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
        <behavior name="WebApplicationTest.WCFProxy.EntityDataFieldCollectionProxyAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
        <behavior name="WebApplicationTest.WCFProxy.Service1AspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
        <behavior name="WebApplicationTest.WCFProxy.ContactProxyAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="WebApplicationTest.WCFProxy.EmployeeProxy"  behaviorConfiguration="MyServiceTypeBehaviors" >
        <endpoint address="" behaviorConfiguration="WebApplicationTest.WCFProxy.EmployeeProxyAspNetAjaxBehavior"
          binding="webHttpBinding" contract="WebApplicationTest.WCFProxy.EmployeeProxy" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
      <service name="WebApplicationTest.WCFProxy.EntityDataFieldCollectionProxy"  behaviorConfiguration="MyServiceTypeBehaviors" >
        <endpoint address="" behaviorConfiguration="WebApplicationTest.WCFProxy.EntityDataFieldCollectionProxyAspNetAjaxBehavior"
          binding="webHttpBinding" contract="WebApplicationTest.WCFProxy.EntityDataFieldCollectionProxy" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
      <service name="WebApplicationTest.WCFProxy.Service1">
        <endpoint address="" behaviorConfiguration="WebApplicationTest.WCFProxy.Service1AspNetAjaxBehavior"
          binding="webHttpBinding" contract="WebApplicationTest.WCFProxy.Service1" />
      </service>
      <service name="WebApplicationTest.WCFProxy.ContactProxy" behaviorConfiguration="MyServiceTypeBehaviors" ><!--new-->
        <endpoint address="" behaviorConfiguration="WebApplicationTest.WCFProxy.ContactProxyAspNetAjaxBehavior"
          binding="webHttpBinding" contract="WebApplicationTest.WCFProxy.ContactProxy" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
    </services>
    <bindings />
    <client />
  </system.serviceModel>
</configuration>

这是REST服务吗?我们需要更多的信息(合同,配置等)... - Ladislav Mrnka
谢谢。我刚刚发布了更多信息。 - Luis Carlos Chavarría
似乎IIS阻止了POST(或其他)请求。 - leppie
今天早些时候我遇到了这个问题,结果发现是由于WebDAV引起的。请发布您的堆栈跟踪或显示哪个模块抛出了错误。 - Darren Kopp
4个回答

1

为了允许跨域ajax调用,您需要:

1) 首先配置您的Web服务器以允许来源和标头 2) 允许使用的请求方法

看起来您已经完成了第一步,也许对于第二步,您只需要进行更改:

[WebGet()]

目标:

[WebInvoke(Method = "*")]
  • 因为尽管您执行了POST或GET请求,Chrome和Firefox始终发送OPTIONS

这对我实际起作用了。IE11还会发送CORS Preflight请求的OPTIONS。 - ohmusama

0

您的服务出现了一些奇怪的问题。首先,我认为insertEntityDate不应该被调用,因为它缺少WebGetWebInvoke属性。另一个奇怪的事情是,getById被定义为WebGet,但它被称为POST JSON请求 - 我刚刚用FireBug和Fiddler检查了一下:

POST http://luiscarlosch.com/WCFProxy/ContactProxy.svc/getByID HTTP/1.1
Host: luiscarlosch.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.16) Gecko/20101130 Firefox/3.5.16 ( .NET CLR 3.5.30729; .NET4.0E)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
X-Requested-With: XMLHttpRequest
Content-Type: application/json; charset=utf-8
Referer: http://luiscarlosch.com/WebFormClean.aspx
Content-Length: 15
Cookie: ASP.NET_SessionId=puzd3ulsj4em4ufd21b4lkjr
Pragma: no-cache
Cache-Control: no-cache

{"IDContact":1}

这项服务对我来说是有效的,但它并不是你向我们展示的合同所描述的同一项服务。顺便说一下,如果你不打算使用SOAP端点,那么没有理由启用serviceMetadata行为并添加Mex端点。


0
<system.webServer>
<handlers> 
<remove name="WebDAV" />
        <add name="RestProxy32" path="Service.svc" verb="*" modules="IsapiModule"
            scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"
            preCondition="classicMode,runtimeVersionv2.0,bitness32"/>
        <add name="RestProxy64" path="Service.svc" verb="*" modules="IsapiModule"
            scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll"
            preCondition="classicMode,runtimeVersionv2.0,bitness64"/>
</handlers> 
<modules>
<remove name="WebDAVModule" />
</modules>

</system.webServer>

0

使用CORS时,规范要求浏览器“预检”请求,通过HTTP OPTIONS向服务器请求支持的方法,服务器发送405方法不允许响应,同时指示允许的方法。

此响应的唯一目的是帮助您找出特定URL资源的通信选项。允许客户端确定与资源相关的选项和/或要求,或服务器的功能,而无需涉及数据传输的特定操作。

希望对您有所帮助,


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接