我有一个Web Api2方法,它返回HttpResponseMessage
public HttpResponseMessage Tokenize(Tokenize tokenize)
{
string requesterIPAddress = "";
try
{
// Get Requester IP Address
RequestIPService requestIPService = new RequestIPService();
requesterIPAddress = requestIPService.GetRequesterIP();
//generate token and insert into Token.TokenStore table and return result
TokenizerService tokenizerService = new TokenizerService();
string token = tokenizerService.Tokenizer(tokenize.APIKey, tokenize.MerchantID, tokenize.StoreValue, tokenize.StoreType);
return Request.CreateResponse(HttpStatusCode.OK, token);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
现在我有一个客户端要求,我必须在Action结果中调用此API方法
[HttpPost]
public ActionResult Index(Tokenize tokenize)
{
TokenizeController tokenizeApi = new TokenizeController();
HttpResponseMessage response = tokenizeApi.Tokenize(tokenize);
return View(response);
}
但是在最后一行APi方法的试块中,即
return Request.CreateResponse(HttpStatusCode.OK, token);
它给我一个错误值不能为空
虽然API方法适用于Ajax Call
任何人都可以告诉如何返回HttpResponseMessage
答案 0 :(得分:0)
使用new HttpResponseMessage()
代替Request.CreateResponse
。你将失去的唯一功能是conneg。如果您使用new StringContent(token)
作为内容,则响应将返回text/plain
。