如何从URL传递wcf restful服务中的字符串参数

时间:2016-03-15 05:54:57

标签: .net web-services wcf rest c#-4.0

我是wcf服务的新手。我开发了一个简单的休息服务。我需要将字符串数据插入从URL传递的数据库表。 请参考以下代码。

RewriteEngine on

RewriteOptions inherit

RewriteCond %{HTTP_HOST} ^hardworkerz\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.hardworkerz\.net$
RewriteRule ^/?$ "https\:\/\/103\.7\.129\.132\/CorporateUI" [R=301,L]

在svc文件中实现AddBook方法,将数据插入表中。

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddBook/{name}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
ServiceResponse AddBook(string name);

当我尝试通过URL传递字符串数据时,如下所示,我收到错误,因为找不到终点。

  

http://localhost/WCF_Entity_Books/BookService.svc/AddBook?name=Family

请在下面找到我的web.config

public ServiceResponse AddBook(string name)
{
    ServiceResponse sobj = new ServiceResponse();
    try
    {
        if (name != null)
        {
            using(SampleContext sc = new SampleContext())
            {
                book book = new book();
                book.BookName = name.ToString();
                sc.books.Add(book);
                sc.SaveChanges();
                sobj.packet.header.responseCode = BookUtility.GNRL_SUCCESS_CODE;
                sobj.packet.header.responseStatus = BookUtility.SUCCESS_STATUS;
                sobj.packet.header.responseMessage = "";
            }
        }
        else
        {
            sobj.packet.header.responseCode = BookUtility.GNRL_INVALID_INPUT_CODE;
            sobj.packet.header.responseStatus = BookUtility.ERROR_STATUS;
            sobj.packet.header.responseMessage = UtilityResource.ResourceManager.GetString(BookUtility.GNRL_INVALID_INPUT_CODE);
        }
    }
    catch (Exception ex)
    {
        throw new FaultException(ex.Message);
    }
    return sobj;
}

0 个答案:

没有答案