2011-02-03 234 views
6

編輯:摘要:似乎我的網絡應用程序可以被任何人(只有Firefox或鉻)和我使用我的主計算機訪問。如果我嘗試從我的任何其他LAN訪問http://luiscarlosch.com/WebFormClean.aspx,它會得到一個405錯誤405:不允許的方法

我可以完美地從本地主機調用WCF Web方法。我發佈到此服務器:http://luiscarlosch.com/WebFormClean.aspx(僅限Firefox或Chrome) 與Visual Studio發佈工具,它工作正常。問題是何時嘗試從另一臺計算機訪問它。我得到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> 
+0

它是REST服務嗎?我們需要更多的信息(合同,配置等)... – 2011-02-03 22:43:18

+0

Thankx。我只是發佈一些更多的信息。 – 2011-02-03 23:17:12

回答

1

爲了允許跨域AJAX調用需要

1)首先配置Web服務器允許的起源和接頭 2)允許使用的方法,使請求

這似乎你已經完成了第一點,也許第二,你只需要改變:

[WebGet()] 

到:

[WebInvoke(Method = "*")] 
  • 因爲雖然你執行一個POST或GET請求。Chrome和Firefox始終發送選項
0

奇怪的東西在你的服務正在發生的事情。首先,我認爲insertEntityDate不應該被調用,因爲它缺少WebGetWebInvoke屬性。另一個奇怪的是getById被定義爲WebGet,但它被稱爲POST JSON請求 - 我只是螢火和提琴手檢查的話:

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

當使用CORS該瀏覽器「預檢」的要求,徵求來自服務器支持的方法與HTTP OPTIONS說明書任務和服務器發送一個405方法不允許響應,也指示所允許的方法。

這種反應的唯一目的是幫助你找出適用於一個特定網址資源的通訊選擇。允許客戶端確定與資源相關的選項和/或要求或服務器的功能,而不需要涉及數據傳輸的特定操作。

希望它能幫助,

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>