2011-02-03 263 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>