从第三方网站发布到我的ASP.NET MVC应用程序

时间:2012-04-20 12:49:31

标签: asp.net-mvc-3 http-post payment-gateway

我正在测试支付提供商(SagePay),并且作为流程的一部分,他们的服务器POST到我的网站并期待响应。我无法使用MVC来实现这一点。

我设置了一个经典的asp测试响应页面并将其添加到我的MVC应用程序中:

<%
Response.Buffer = True 
response.Clear()
response.contenttype="text/plain"
response.write "Status=OK" & vbCRLF
response.write "RedirectURL=http://www.redirectsomewhere.co.uk" & vbCRLF
response.End()
%>

这项工作很好。

然而,当我尝试对MVC做同样的事情时,它不起作用:

控制器:

[HttpPost]
public ActionResult TestCallback()
{
     return View();
}

查看:

@{
    Response.Buffer = true;
    Response.Clear();
    Response.ContentType = "text/plain";
    Response.Write("Status=OK" + System.Environment.NewLine);
    Response.Write("RedirectURL=http://www.redirectsomewhere.co.uk" + System.Environment.NewLine);
    Response.End();
}

错误消息是来自支付提供商的一般错误,因此没有真正的帮助,但我已将错误缩小到页面呈现的位置。

我可以很好地浏览两个页面(我需要从MVC控制器方法中删除HttpPost属性),并且两个页面都显示相同的数据。

这是支付提供商POST到的MVC网址:

http://myipaddress/CA_UAT/Token/TestCallback

这是经典的asp URL工作正常:

http://myipaddress/CA_UAT/Token/TestCallback.asp

我为asp页面创建了一个'Token'目录,因此url会匹配用于测试目的。

我做错了什么?

更新

为了回应Hari的评论,我安装了一个名为'Header Spy'的Firefox插件,它给了我这个信息:

Response HTTP/1.1 200 OK
Source: Response
HttpHeader:Server
Request:User-Agent Cookie
Response:Response Date Set-Cookie

两个页面都显示相同的信息。

4 个答案:

答案 0 :(得分:1)

您无需返回操作结果即可将纯文本发送回屏幕。完成此操作的最简单方法是返回字符串值。用以下内容替换控制器中的代码。

[HttpPost]
public string TestCallback()
{
  string result = "Status=OK";
  result += System.Environment.NewLine;
  result += "RedirectURL=http://www.redirectsomewhere.co.uk";
  result += System.Environment.NewLine;
  return result;
}

这将不会返回您在字符串中所拥有的其他响应。通过使用ActionResult和View,您可能会从主视图返回标记。

答案 1 :(得分:0)

我没有在视图中编写响应,而是将其写入动作方法中,如下所示:

[HttpPost]
public ActionResult TestCallback()
{
  Response.Buffer = true;
  Response.Clear();
  Response.ContentType = "text/plain";
  Response.Write("Status=OK" + System.Environment.NewLine);
  Response.Write("RedirectURL=http://www.redirectsomewhere.co.uk" + System.Environment.NewLine);
  Response.Flush();
  return new EmptyResult();
}

返回EmptyResult时,您将确保MVC不会向响应添加任何内容。

答案 2 :(得分:0)

试试这样:

[HttpPost]
public ActionResult TestCallback()
{
    var sb = new StringBuilder();
    sb.AppendLine("Status=OK");
    sb.AppendLine("RedirectURL=http://www.redirectsomewhere.co.uk");
    return Content(sb.ToString(), "text/plain");
}

或以更MVCish的方式:

查看型号:

public class ResponseViewModel
{
    public string Status { get; set; }
    public string RedirectUrl { get; set; }
}

然后是自定义操作结果:

public class StatusActionResult : ContentResult
{
    private readonly ResponseModel _model;
    public StatusActionResult(ResponseModel model)
    {
        _model = model;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "text/plain";
        response.Write(string.Format("Status={0}{1}", _model.Status, Environment.NewLine));
        response.Write(string.Format("RedirectURL={0}", _model.RedirectUrl));
    }
}

最后你的控制器动作:

[HttpPost]
public ActionResult TestCallback()
{
    var model = new ResponseModel
    {
        Status = "OK",
        RedirectUrl = "http://www.redirectsomewhere.co.uk"
    };
    return new StatusActionResult(model);
}

答案 3 :(得分:0)

我想知道sagepay是否期望文件扩展名......我在继承人身上做了某种URL验证。你知道你的Action是否被调用了吗?

还尝试添加一条路线,使您的mvc网址看起来像“TestCallback.asp”。