我有另一个与WCF相关的问题,虽然这看起来相对简单。
我有一个看起来像这样的方法:
[OperationContract()]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, UriTemplate = "/BS/vFacility/{vFacility}/vAdress/{vAdress}/vPostalNr/{vPostalNr})]
string BS(string vFacility, string vAdress, string vPostalNr);
如果我这样称呼它,这是有效的:
/BS/vFacility/Oven/vAdress/Boulevard 25/vPostalNr/6749
但我希望用户能够通过网址调用此方法,然后不输入,就像地址一样:
/BS/vFacility/Oven/vAdress//vPostalNr/6749
在这种情况下,如果可能的话,我希望忽略的参数设置为null。 如果我尝试上面的网址,我会收到一条消息,指出“找不到端点”
这是我的web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<pages validateRequest="false" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors >
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WCFBolig.Service1" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding"
contract="WCFBolig.IService1"
behaviorConfiguration="web"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
<connectionStrings>
<add
name="UnikBoligCon"
connectionString="server=XX;database=XX;user=XX;password=XX"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
</configuration>
这是可能的还是用户被迫为所有参数提供输入?
答案 0 :(得分:0)
我只需要了解一下URI来做到这一点。结果我不得不改变它:
/BS/vFacility/{vFacility}/vAdress/{vAdress}/vPostalNr/{vPostalNr}
To This:
/BS?vFacility={vFacility}&vAdress={vAdress}&vPostalNr={vPostalNr}
现在我可以简单地忽略参数,它仍然有效。必须确保SQL代码仍然可以使用空值。
F.eks。像这样的网址(http://localhost/BAlias/Service1.svc/BS?)或者这个(http://localhost/BAlias/Service1.svc/BS?vAdress=Whateverroad)现在工作得很好。