6

编辑:摘要:似乎任何人(只有 firefox 或 chrome)都可以访问我的网络应用程序,并且我可以使用我的主计算机访问我的网络应用程序。如果我尝试从我的任何其他 LAN 访问http://luiscarlosch.com/WebFormClean.aspx,则会收到 405 错误

我可以完美地从 localhost 调用 WCF Web 方法。我使用 Visual Studio 发布工具发布到此服务器: http: //luiscarlosch.com/WebFormClean.aspx(仅 firefox 或 chrome),它工作正常。问题是当尝试从另一台计算机访问它时。我得到 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,因为他们更改了值。所以。有些客户可以阅读这些方法,但有些则不能。这应该发生。

网页配置

<?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>
4

4 回答 4

1

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

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

看来你已经完成了第一点,也许第二点你只需要改变:

[WebGet()]

到:

[WebInvoke(Method = "*")]
  • 因为尽管您执行 POST 或 GET 请求。Chrome 和 Firefox 总是发送 OPTIONS
于 2014-01-27T12:41:52.913 回答
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 端点。

于 2011-02-04T08:39:30.770 回答
0

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

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

希望能帮助到你,

于 2012-05-24T23:03:46.030 回答
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>
于 2013-08-26T15:00:02.257 回答