我用swagger 2.0制作了一个C#客户端,在editor.swagger.io中的yamel如下:
swagger: "2.0"
info:
description: "This is a web service we call it XZ Company for getting Milage of the Customers "
version: "1.0.0"
title: "XZ"
termsOfService: "http://ws.smtallk.org/XZ/terms/"
contact:
email: "m@gmail.com"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "ws.smtallk.org"
basePath: "/"
schemes:
- "http"
- "https"
paths:
/customers:
post:
tags:
- "Customers"
summary: "Create Customer"
description: "IdentificationType => 1: National ID 2: Passport 3:Other Identification Types"
operationId: "createCustomer"
consumes:
- 'text/plain; charset=utf-8'
- 'application/json'
produces:
- "application/json"
parameters:
- in: "body"
name: "body"
description: "Create Customer object"
required: true
schema:
$ref: "#/definitions/Customer_Body"
responses:
200:
description: ''
schema:
$ref: '#/definitions/Customer_Response'
400:
description: ''
schema:
$ref: '#/definitions/Error400'
definitions:
Customer_Body:
type: "object"
properties:
identificationNo:
type: "string"
description: "National ID or Passport Number or any other Identification Number"
identificationType:
type: "integer"
format: "int32"
description: "1:National ID , 2: passport , 3:others"
firstName:
type: "string"
description: ""
lastName:
type: "string"
description: ""
province:
type: "string"
description: ""
city:
type: "string"
description: ""
address:
type: "string"
description: ""
phone:
type: "string"
description: ""
Customer_Response:
type: "object"
properties:
message:
type: "string"
description: ""
result:
$ref: '#/definitions/ResultInCustomer'
error:
$ref: '#/definitions/error'
ResultInCustomer:
type: "object"
properties:
_key:
type: "string"
description: ""
_id:
type: "string"
description: ""
_rev:
type: "string"
description: ""
identificationNo:
type: "string"
description: ""
identificationType:
type: "integer"
format: "int32"
description: ""
firstName:
type: "string"
description: ""
lastName:
type: "string"
description: ""
province:
type: "string"
description: ""
city:
type: "string"
description: ""
address:
type: "string"
description: ""
phone:
type: "string"
description: ""
isDeleted:
type: "boolean"
description: ""
vehicles:
type: array
items:
$ref: '#/definitions/VehicleInResult'
VehicleInResult:
type: "object"
properties:
_id:
type: "string"
description: ""
vin:
type: "string"
description: ""
chassis:
type: "string"
description: ""
engine:
type: "string"
description: ""
plate:
type: "string"
description: ""
identificationNo:
type: "string"
description: ""
customer:
$ref: '#/definitions/customer'
我有一个asp.net mvc项目,并且将swagger客户端引用到了该mvc。我用以下操作将此招摇摇叫的客户称为:
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;
namespace Panel.Controllers
{
public class HomeController : Controller
{
public ActionResult RegisterCustomer()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RegisterCustomer([Bind(Include = "identificationNo,identificationType,firstName,lastName,province,city,address,phone")] RegisterCustomerViewModel register)
{
if (ModelState.IsValid)
{
CustomersApi a = new CustomersApi();
IO.Swagger.Model.CustomerBody customerBody = new IO.Swagger.Model.CustomerBody
{
IdentificationNo = register.identificationNo,
IdentificationType = register.identificationType,
FirstName = register.firstName,
LastName = register.lastName,
Province = register.province,
City = register.city,
Address = register.address,
Phone = register.phone
};
Debug.WriteLine(customerBody.ToJson());
Tuple<object, int> CreateCustomerResponse = a.CreateCustomer(customerBody);
if (CreateCustomerResponse .Item2 == 200)
{
CustomerResponse result1 = (CustomerResponse)b.Item1;
SamanTel_DBEntities db = new SamanTel_DBEntities();
Customer customer = new Customer
{
PublicId = Guid.NewGuid(),
CreateDate = DateTime.Now,
ChangeDate = DateTime.Now,
RegistererUser = db.Users.Where(u => u.Username == User.Identity.Name).Select(u => u.Id).SingleOrDefault(),
ModifierUser = db.Users.Where(u => u.Username == User.Identity.Name).Select(u => u.Id).SingleOrDefault(),
IsDeleted = result1.Result.IsDeleted.Value,
C_key = result1.Result.Key,
C_id = result1.Result.Id,
C_rev = result1.Result.Rev,
identificationNo = result1.Result.IdentificationNo,
identificationType = result1.Result.IdentificationType.Value,
FirstName = result1.Result.FirstName,
LastName = result1.Result.LastName,
Province = result1.Result.Province,
City = result1.Result.City,
Address = result1.Result.Address,
Phone = result1.Result.Phone,
};
try
{
db.Customer.Add(customer);
db.SaveChanges();
}
catch (Exception e)
{
throw;
}
}
else
{
}
}
return View(register);
}
}
}
在“ CreateCustomerResponse”变量的mvc post操作中,我看到一个错误,该错误显示如下图所示的“ identificationNo is needed”,但在“ RegisterCustomer”操作的输入中,“ identificationNo”具有值,但我不知道是什么问题?!
我使用“ editor.swagger.io”测试了此输入,然后使用邮递员进行了测试,并且我收到的响应没有问题。我该如何解决我的问题?