简体   繁体   中英

How to get all the URLTemplate of method names from the WCF Rest service in C#

I have a rest service URL as below http://XXXXXXXX/RestServices/Project.svc

This SVC has two methods,

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "UserLogin")]
    ResultInfo Login(Login objLogin);

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "SaveUser")]
    ResultInfo User(User objUser);

The Method Name are Login and User, but the UriTemplate Names are UserLogin and SaveUser accordingly.

I want to get the Output in C# list as below

UserLogin
SaveUser

Need your help on this.

You can get the Method names by using the following method

In ASPX Page:

<asp:ListBox ID="lstMethodName" runat="server" Visible="true" AutoPostBack="true"
        Style="height: 150px; width:300px; overflow: auto;"></asp:ListBox>

In Code Behind:

string wsdlUrl = "http://XXXXXXXX/RestServices/Project.svc?wsdl"; // WCF wsdl address
                XmlTextReader myReader = new XmlTextReader(wsdlUrl);
                if (ServiceDescription.CanRead(myReader))
                {
                    ServiceDescription myDescription = ServiceDescription.Read(myReader);

                    foreach (PortType pt in myDescription.PortTypes)
                    {
                        foreach (Operation op in pt.Operations)
                        {
                            lstMethodName.Items.Add(op.Name);
                        }
                    }
                }

Add the Namespace :

 using System.Web.Services.Description;
 using System.Collections.Generic;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM