Search code examples
c#wcfurl

WCF url allow user not to input parameter


I have another WCF related question although this one looks relatively simple.

I have a method that looks like this:

[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);

This works if I call it like this:

/BS/vFacility/Oven/vAdress/Boulevard 25/vPostalNr/6749

but I would for the user to be able to call this method through an url and then not input, like an adress, like this:

/BS/vFacility/Oven/vAdress//vPostalNr/6749

In this case I would like the ignored parameter to be set to null if possible. If I try the above url I get a message stating "endpoint was not found"

This is my 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>

Is this possible or are users forced to giving inputs for all parameters?


Solution

  • I just needed to learn a bit about URIs to do this. Turns out I had to change it from this:

    /BS/vFacility/{vFacility}/vAdress/{vAdress}/vPostalNr/{vPostalNr}
    

    To This:

    /BS?vFacility={vFacility}&vAdress={vAdress}&vPostalNr={vPostalNr}
    

    Now I can simply ignore parameters and it still works. Do have to make sure the SQL code will still work with null values though.

    F.eks. urls like this(http://localhost/BAlias/Service1.svc/BS?) or this(http://localhost/BAlias/Service1.svc/BS?vAdress=Whateverroad) now work just fine.